简体   繁体   中英

ListView clickable

hello guys I'm looking how to make my ListView clickable , i searched in the net but i haven't found the right answer , and this is my code please help me

`public class acceuil extends AppCompatActivity { ListView listView; int [] movie_poster_resource = {R.drawable.profil}; String[] patient_names; String[] temps_rendez; MovieAdapter adapter; View view; Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_acceuil);
    listView= (ListView)findViewById(R.id.listView);
    temps_rendez = getResources().getStringArray(R.array.temps);
    patient_names = getResources().getStringArray(R.array.patient_title);
    int i=0;
    adapter = new MovieAdapter(getApplicationContext(),R.layout.patient_name);
    listView.setAdapter(adapter);
    for (String titles: patient_names)
    {

        MovieDataProvider dataProvider = new MovieDataProvider(movie_poster_resource[i],titles,temps_rendez[i]);
        adapter.add(dataProvider);
    }




}

public void onItemClick(AdapterView<?> l, View v, int position, long id) {

    if (id == 0)
    startActivity(new Intent(this, patient_from_listview.class));
}








public void open_messagerie (View view){
    startActivity(new Intent(this, acceuil.class));
}
public void openn_otification (View view){
    startActivity(new Intent(this, acceuil.class));
}
public void opena_parametre (View view){
    startActivity(new Intent(this, acceuil.class));
}
public void open_calcule (View view){
    startActivity(new Intent(this, acceuil.class));
}

}`

You'd call listView.setOnItemClickListener(OnItemClickListener) . That sets the class to be called when an item is clicked. It looks like you've already implemented the onItemClicked function, this will hook it up.

This is how you can achieve this

adapter = new MovieAdapter(getApplicationContext(),R.layout.patient_name);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position){
// HERE YOU CAN MAKE CASES FOR EACH CLICK
}

Here is my code, how to do clickable listview on Android Studio...

MAIN ACTIVICTY CODE

ListView listView;

int mImage[] = {R.drawable.switzerland, R.drawable.canada, R.drawable.japan, R.drawable.usa};
String mTitle[] = {"Switzerland Title", "Canada Title", "Japan Title", "USA Title"};
String mDescription[] = {"Switzerland is a mountainous Central European country, home to numerous lakes, villages and the high peaks of the Alps.", "Canada is a country in the northern part of North America. Its ten provinces and three territories extend from the Atlantic to the Pacific.", "Japan is an island country in East Asia, located in the northwest Pacific Ocean.", "The U.S. is a country of 50 states covering a vast swath of North America, with Alaska in the northwest."};

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


    // for listview settings
    listView = findViewById(R.id.listview);

    MyAdapter adapter = new MyAdapter(this, mImage, mTitle, mDescription);
    listView.setAdapter(adapter);

    //listview click listener
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            if (position==0){
                Intent intent = new Intent(getApplicationContext(), Switzerland.class);
                Bundle bundle = new Bundle();
                bundle.putInt("image", mImage[0]);
                intent.putExtras(bundle);

                intent.putExtra("title", mTitle[0]);
                intent.putExtra("description", mDescription[0]);
                intent.putExtra("position", ""+0);
                startActivity(intent);

            }else if (position==1){
                Intent intent = new Intent(getApplicationContext(), Canada.class);
                Bundle bundle = new Bundle();
                bundle.putInt("image", mImage[1]);
                intent.putExtras(bundle);

                intent.putExtra("title", mTitle[1]);
                intent.putExtra("description", mDescription[1]);
                intent.putExtra("position", ""+1);
                startActivity(intent);

            }else if (position==2){
                Intent intent = new Intent(getApplicationContext(), Japan.class);
                Bundle bundle = new Bundle();
                bundle.putInt("image", mImage[2]);
                intent.putExtras(bundle);

                intent.putExtra("title", mTitle[2]);
                intent.putExtra("description", mDescription[2]);
                intent.putExtra("position", ""+2);
                startActivity(intent);

            }else if (position==3){
                Intent intent = new Intent(getApplicationContext(), USA.class);
                Bundle bundle = new Bundle();
                bundle.putInt("image", mImage[3]);
                intent.putExtras(bundle);

                intent.putExtra("title", mTitle[3]);
                intent.putExtra("description", mDescription[3]);
                intent.putExtra("position", ""+3);
                startActivity(intent);

            }
        }
    });

}

// for listview adapter
class MyAdapter extends ArrayAdapter<String> {

    Context context;
    int sImage[];
    String sTitle[];
    String sDescription[];

    MyAdapter (Context c, int image[], String title[], String description[]){
        super(c, R.layout.main_page_row, R.id.main_page_title, title);
        this.context = c;
        this.sImage = image;
        this.sTitle = title;
        this.sDescription = description;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view= layoutInflater.inflate(R.layout.main_page_row, parent, false);

        ImageView imageView = view.findViewById(R.id.main_page_image);
        TextView titleText = view.findViewById(R.id.main_page_title);
        TextView descriptionText = view.findViewById(R.id.main_page_description);

        imageView.setImageResource(sImage[position]);
        titleText.setText(sTitle[position]);
        descriptionText.setText(sDescription[position]);

        return view;

    }
}

Here is MAIN ACTIVITY XML Code

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1500dp"
        android:orientation="vertical">

        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>

Here is MAIN ACTIVITY ROW XML Code

<androidx.cardview.widget.CardView 
android:orientation="vertical"
app:cardElevation="5dp"
app:cardCornerRadius="12dp"
android:layout_margin="3dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageID"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@drawable/erroricon"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/titleID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:text="@string/bb_name"
        android:textSize="18sp"
        android:textColor="#000000"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/descriptionID"
        android:text="@string/mp_des"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="10dp"
        android:textColor="#000000"
        android:textSize="16sp"
        android:ellipsize="end"
        android:maxLines="3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

</androidx.cardview.widget.CardView>

Here is Details Page Java Code

ImageView image;
TextView sTitle, sDescription;
int position;

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

    image = findViewById(R.id.details_page_image);
    sTitle = findViewById(R.id.details_page_title);
    sDescription = findViewById(R.id.details_page_description);

    if (position == 0){
        Intent intent = getIntent();

        Bundle bundle = this.getIntent().getExtras();
        int picture = bundle.getInt("image");
        String postTitle = intent.getStringExtra("title");
        String postDescrip = intent.getStringExtra("description");

        image.setImageResource(picture);
        sTitle.setText(postTitle);
        sDescription.setText(postDescrip);

    }

    if (position == 1){
        Intent intent = getIntent();

        Bundle bundle = this.getIntent().getExtras();
        int picture = bundle.getInt("image");
        String postTitle = intent.getStringExtra("title");
        String postDescrip = intent.getStringExtra("description");

        image.setImageResource(picture);
        sTitle.setText(postTitle);
        sDescription.setText(postDescrip);

    }

    if (position == 2){
        Intent intent = getIntent();

        Bundle bundle = this.getIntent().getExtras();
        int picture = bundle.getInt("image");
        String postTitle = intent.getStringExtra("title");
        String postDescrip = intent.getStringExtra("description");

        image.setImageResource(picture);
        sTitle.setText(postTitle);
        sDescription.setText(postDescrip);

    }

    if (position == 3){
        Intent intent = getIntent();

        Bundle bundle = this.getIntent().getExtras();
        int picture = bundle.getInt("image");
        String postTitle = intent.getStringExtra("title");
        String postDescrip = intent.getStringExtra("description");

        image.setImageResource(picture);
        sTitle.setText(postTitle);
        sDescription.setText(postDescrip);

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM