你好我们正在寻找如何使我的ListView可点击,我在网上搜索但我找不到正确的答案,这是我的代码请帮助我
`public class acceuil扩展AppCompatActivity { ListView listView; int [] movie_poster_resource = {R.drawable.profil}; String [] patient_names; String [] temps_rendez; MovieAdapter适配器; 查看视图; 意图;
@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));
}
}`
答案 0 :(得分:0)
你打电话给listView.setOnItemClickListener(OnItemClickListener)
。这设置了在单击项目时要调用的类。看起来你已经实现了onItemClicked函数,这将把它连接起来。
答案 1 :(得分:0)
这就是你如何实现这个目标
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
}
答案 2 :(得分:0)
这是我的代码,如何在 Android Studio 上执行可点击的列表视图...
主要活动代码
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;
}
}
这里是主要活动 XML 代码
<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>
这里是主要活动行 XML 代码
<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>
这是详细信息页面 Java 代码
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);
}
}