我尝试做一个简单的ListView
,但我无法点击它。
其他一切都很好。
也许我在setOnItemClickListener
中做错了什么?
这是我的代码:
package com.roa.demolistview;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Project> mProject = new ArrayList<Project>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo_listview);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
populateCarList();
populateListView();
listClicked();
}
private void populateCarList() {
mProject.add(new Project("cool car!!", "very nice", R.drawable.coolcar, Uri.parse("http://roish2.wix.com/roish-2#!אחלה מכונית.jpg/zoom/cy2g/image_1bj2")));
mProject.add(new Project("cool poster", "nice job!", R.drawable.coolproj, Uri.parse("http://roish2.wix.com/roish-2#!/zoom/cy2g/imagecw0")));
mProject.add(new Project("Red car!!", "smooth car!", R.drawable.coolredcar, Uri.parse("http://roish2.wix.com/roish-2#!/zoom/cy2g/image1y9x")));
mProject.add(new Project("A ship!!", "smooth ship!", R.drawable.coolship, Uri.parse("http://roish2.wix.com/roish-2#!2015.jpg/zoom/cy2g/image_1mk3")));
mProject.add(new Project("A plain!!", "smooth plain!", R.drawable.cooltransport, Uri.parse("http://roish2.wix.com/roish-2#!Transport 3.jpg/zoom/cy2g/image_12gg")));
mProject.add(new Project("Yellow car!!", "smooth car!", R.drawable.coolyellowcar, Uri.parse("http://roish2.wix.com/roish-2#!/zoom/cy2g/image19cg")));
}
private void populateListView() {
ArrayAdapter<Project> adapter = new MyListAdapter();
ListView list = (ListView)findViewById(R.id.listView);
list.setAdapter(adapter);
}
private void listClicked() {
ListView list = (ListView)findViewById(R.id.listView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Project proj = mProject.get(position);
String message = "you clicked on: " + proj.getName();
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
});
}
private class MyListAdapter extends ArrayAdapter<Project> {
public MyListAdapter(){
super(MainActivity.this, R.layout.item_view, mProject);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
}
//find the project to work with
Project currentProject = mProject.get(position);
//fill the view
ImageView imageView = (ImageView)itemView.findViewById(R.id.imageView);
TextView nameText = (TextView)itemView.findViewById(R.id.item_nameText);
TextView describe = (TextView)itemView.findViewById(R.id.item_descrText);
imageView.setImageResource(currentProject.getIcon());
nameText.setText(currentProject.getName());
describe.setText(currentProject.getDescription());
return itemView;
}
}
}
答案 0 :(得分:2)
您有ListView
的副本。
如果没有listClicked
方法,请执行此操作:
private void populateListView() {
ArrayAdapter<Project> adapter = new MyListAdapter();
ListView list = (ListView)findViewById(R.id.listView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Project proj = mProject.get(position);
String message = "you clicked on: " + proj.getName();
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
});
list.setAdapter(adapter);
}
答案 1 :(得分:0)
您的click方法未被调用,请按如下所示更新您的populateListView()并尝试
private void populateListView() {
ArrayAdapter<Project> adapter = new MyListAdapter();
ListView list = (ListView)findViewById(R.id.listView);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Project proj = mProject.get(position);
String message = "you clicked on: " + proj.getName();
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
});
}
答案 2 :(得分:0)
确定。谢天谢地,我发现了问题。
我需要做的就是在XML布局中将行android:focusable="true"
更改为false