1 year ago

#284634

test-img

Taygun

Showing Image From SQLite Database

i developed a new app which shows my diecast car database from SQLite Database (via this link https://www.youtube.com/watch?v=5_jyEGe6ZHo). Everything is fine i can see infos which are in database. One of this information is the image links of the cars. I can show links. But I couldn't make the links clickable.

If I want to add a link manually regardless of the database, I can do it clickable. But I am very confused because I have pulled the data from the SqLite database. I want to make the link marked in the photo clickable. The codes are below. Thank you for your help already.

Photo:https://live.staticflickr.com/65535/51935175248_f821d83ba0_z.jpg

layout_item.xaml :

         <androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="8dp"
    android:layout_margin="8dp"
    android:background="#E0E0E0"
    android:orientation="horizontal"
    >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_margin="8dp"
        android:background="#E0E0E0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        >

    <ImageView
        android:src="@drawable/hww"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="#E0E0E0"
        android:layout_marginTop="9dp"
        android:contentDescription="@string/todo" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_weight="9"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#E0E0E0">

        <TextView
            android:id="@+id/model"
            android:layout_marginStart="10dp"
            android:gravity="center_vertical|start"
            android:textAllCaps="true"
            android:textStyle="bold"
            android:textColor="#FF8000"
            android:text="@string/_16_cadillac_ats_v"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tip"
            android:layout_marginStart="10dp"
            android:gravity="center_vertical|start"
            android:textColor="#FF8000"
            android:textStyle="italic"
            android:text="@string/regular"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/renk"
            android:layout_marginStart="10dp"
            android:gravity="center_vertical|start"
            android:textColor="#FF8000"
            android:textStyle="bold"
            android:text="@string/k_rm_z"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/kod"
            android:layout_marginStart="10dp"
            android:gravity="center_vertical|start"
            android:textColor="#FF8000"
            android:textStyle="italic"
            android:text="@string/_38_250"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/seri"
            android:layout_marginStart="10dp"
            android:gravity="center_vertical|start"
            android:textColor="#FF8000"
            android:textStyle="italic"
            android:text="@string/seri"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true" />

    </LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>

activity_main.xaml :

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#404040"
    tools:ignore="ExtraText">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginStart="20dp"
        tools:ignore="UseCompoundDrawables">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/my_hw_collection"
            android:textStyle="bold"
            android:layout_marginTop="4dp"
            android:layout_marginStart="40dp"
            android:textSize="18sp"
            android:textColor="#FF8000" />

    </LinearLayout>


    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="4dp"
        android:contentDescription="@string/todo"
        android:src="@drawable/xamarin" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginTop="50dp"
        android:background="#C56300">

        <Button
            android:id="@+id/anasayfa_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="34dp"
            android:text="@string/ANASAYFA"
            />

        <Button
            android:id="@+id/model_btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/modeller"
            tools:ignore="HardcodedText" />

        <Button
            android:id="@+id/istek_btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/stekler" />
    </LinearLayout>



    <com.mancj.materialsearchbar.MaterialSearchBar
        android:id="@+id/search_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="110dp"
        app:mt_hint="Arama"
        app:mt_searchBarColor="#C0C0C0"
        app:mt_speechMode="false" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_search"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/search_bar" />

</RelativeLayout>

MainActivity.java:

    public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    SearchAdapter adapter;

    MaterialSearchBar materialSearchBar;
    List<String> suggestList = new ArrayList<>();

    Database database;


    public Button anasayfabtn,modelbtn,istekbtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        anasayfabtn = findViewById(R.id.anasayfa_btn);
        modelbtn = findViewById(R.id.model_btn1);
        istekbtn = findViewById(R.id.istek_btn2);

        istekbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent istek = new Intent(MainActivity.this,MainActivity2.class);
                startActivity(istek);

            }
        });

        anasayfabtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent istek = new Intent(MainActivity.this,MainActivity3.class);
                startActivity(istek);

            }
        });




        recyclerView = (RecyclerView)findViewById(R.id.recycler_search);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);

        materialSearchBar  = (MaterialSearchBar)findViewById(R.id.search_bar);

        database = new Database(this);

        materialSearchBar.setHint("Arama");
        materialSearchBar.setCardViewElevation(10);
        loadSuggestList();
        materialSearchBar.addTextChangeListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                List<String> suggest = new ArrayList<>();
                for(String search:suggestList)
                {

                    if (search.toLowerCase().contains(materialSearchBar.getText().toLowerCase()))
                        suggest.add(search);
                }

                materialSearchBar.setLastSuggestions(suggest);

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        materialSearchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {
            @Override
            public void onSearchStateChanged(boolean enabled) {

                if (!enabled)
                {
                    adapter = new SearchAdapter(getBaseContext(),database.getFriends());
                    recyclerView.setAdapter(adapter);
                }

            }

            @Override
            public void onSearchConfirmed(CharSequence text) {
            startSearch(text.toString());
            }

            @Override
            public void onButtonClicked(int buttonCode) {

            }
        });

        adapter = new SearchAdapter(this,database.getFriends());
        recyclerView.setAdapter(adapter);
    }

    private void startSearch(String text) {

        adapter = new SearchAdapter(this,database.getFriendByModel(text));
        recyclerView.setAdapter(adapter);
    }

    private void loadSuggestList() {

        suggestList = database.getModels();
        materialSearchBar.setLastSuggestions(suggestList);
    }


}

Model: Friend.java:

 public class Friend {

    public int id;
    public String model,tip,renk,kod,seri;

    public Friend(int id, String model, String tip, String renk, String kod, String seri) {
        this.id = id;
        this.model = model;
        this.tip = tip;
        this.renk = renk;
        this.kod = kod;
        this.seri = seri;

    }


    public Friend() {

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getTip() {
        return tip;
    }

    public void setTip(String tip) {
        this.tip = tip;
    }

    public String getRenk() {
        return renk;
    }

    public void setRenk(String renk) {
        this.renk = renk;
    }

    public String getKod() {
        return kod;
    }

    public void setKod(String kod) {
        this.kod = kod;
    }

    public String getSeri() {
        return seri;
    }

    public void setSeri(String seri) {
        this.seri = seri;
    }
}

Database: Database.java:

    public class Database extends SQLiteAssetHelper {

    private static final String DB_NAME="friend.db";
    private static final int DB_VER=1;

    public Database(Context context) {
        super(context, DB_NAME, null,DB_VER);
    }

    public List<Friend> getFriends()
    {

        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        String[] sqlSelect={"Id","Model","Tip","Renk","Kod","Seri"};
        String tableName="Hwlistmodel";

        qb.setTables(tableName);
        Cursor cursor = qb.query(db,sqlSelect, null, null, null, null,null);
        List<Friend> result = new ArrayList<>();
        if(cursor.moveToFirst())
        {

            do{
                Friend friend = new Friend();
                friend.setId(cursor.getInt(cursor.getColumnIndexOrThrow("Id")));
                friend.setModel(cursor.getString(cursor.getColumnIndexOrThrow("Model")));
                friend.setTip(cursor.getString(cursor.getColumnIndexOrThrow("Tip")));
                friend.setRenk(cursor.getString(cursor.getColumnIndexOrThrow("Renk")));
                friend.setKod(cursor.getString(cursor.getColumnIndexOrThrow("Kod")));
                friend.setSeri(cursor.getString(cursor.getColumnIndexOrThrow("Seri")));

                result.add(friend);
            } while (cursor.moveToNext());
        }
        return result;

    }


    public List<String> getModels()

    {

        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        String[] sqlSelect={"Model"};
        String tableName="Hwlistmodel";

        qb.setTables(tableName);
        Cursor cursor = qb.query(db,sqlSelect, null, null, null, null,null);
        List<String> result = new ArrayList<>();
        if(cursor.moveToFirst())
        {

            do{
                result.add(cursor.getString(cursor.getColumnIndexOrThrow("Model")));
            } while (cursor.moveToNext());
        }
        return result;

    }


    public List<Friend> getFriendByModel(String model)

    {

        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        String[] sqlSelect={"Id","Model","Tip","Renk","Kod","Seri"};
        String tableName="Hwlistmodel";

        qb.setTables(tableName);
        Cursor cursor = qb.query(db,sqlSelect, "Model LIKE ?", new String[]{"%"+model+"%"}, null, null,null);
        List<Friend> result = new ArrayList<>();
        if(cursor.moveToFirst())
        {

            do{
                Friend friend = new Friend();
                friend.setId(cursor.getInt(cursor.getColumnIndexOrThrow("Id")));
                friend.setModel(cursor.getString(cursor.getColumnIndexOrThrow("Model")));
                friend.setTip(cursor.getString(cursor.getColumnIndexOrThrow("Tip")));
                friend.setRenk(cursor.getString(cursor.getColumnIndexOrThrow("Renk")));
                friend.setKod(cursor.getString(cursor.getColumnIndexOrThrow("Kod")));
                friend.setSeri(cursor.getString(cursor.getColumnIndexOrThrow("Seri")));

                result.add(friend);
            } while (cursor.moveToNext());
        }
        return result;

    }


}

Adapter: SearchAdapter.java:

  class SearchViewHolder extends RecyclerView.ViewHolder{

    public TextView model,tip,renk,kod,seri;

    public SearchViewHolder(View itemView) {
        super(itemView);
        model = (TextView)itemView.findViewById(R.id.model);
        tip = (TextView)itemView.findViewById(R.id.tip);
        renk = (TextView)itemView.findViewById(R.id.renk);
        kod = (TextView)itemView.findViewById(R.id.kod);
        seri= (TextView)itemView.findViewById(R.id.seri);

    }
}

public class SearchAdapter extends RecyclerView.Adapter<SearchViewHolder>{

    private Context context;
    private List<Friend> friends;

    public SearchAdapter(Context context, List<Friend> friends) {
        this.context = context;
        this.friends = friends;
    }

    @NonNull
    @Override
    public SearchViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemView = inflater.inflate(R.layout.layout_item,parent,false);
        return new SearchViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchViewHolder holder, int position) {

        holder.model.setText(friends.get(position).getModel());
        holder.tip.setText(friends.get(position).getTip());
        holder.renk.setText(friends.get(position).getRenk());
        holder.kod.setText(friends.get(position).getKod());
        holder.seri.setText(friends.get(position).getSeri());

    }

    @Override
    public int getItemCount() {
        return friends.size();
    }
}

image

sqlite

clickable

0 Answers

Your Answer

Accepted video resources