我想设计ListView中的每个列表项都是可以点击的,并且可以选择单击哪个列表项。但它不能。我尝试了两个方法:setOnItemClickListener()和setOnItemSelectedListener()在我的代码上。我已经搜索过有关该文章的几个参考文献,但它仍然无法工作(可点击) 我想发布下面的代码:代码可以显示列表项,我可以看到 Log.d(“mListView01.getCount()=”,“”+ vc)行的Log.d内容; 在LogCat上。但是,如果我点击列表项目,则没有任何回复。 如果你不介意,你能帮我指出我错在哪里,谢谢!
使用Activity Widget创建listView的代码:
......
setContentView(R.layout.main_open);
TextView itemText = (TextView) findViewById(R.id.itemText);
TextView codeText = (TextView) findViewById(R.id.codeText);
itemText.setText(selectedItem);
codeText.setText(selectedCode);
ListView mListView01 = (ListView)findViewById(R.id.main_open_listview1);
String[] keys = new String[] {"title","title_image", "content",
"title1","title1_image","content1","title2","title2_image","content2"};
int[] resValues = new int[] { R.id.title, R.id.title_image, R.id.content,
R.id.title1, R.id.title1_image, R.id.content1,R.id.title2, R.id.title2_image, R.id.content2};
openDocAdapter opendoc = new openDocAdapter(this,localdcoumentlist, R.layout.main_open_content, keys, resValues );
mListView01.setSelected(true);
mListView01.setClickable(true);
mListView01.setAdapter(opendoc);
int vc = mListView01.getCount();
Log.d(" mListView01.getCount()="," "+vc);
mListView01.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Log.d("Selected From setOnItemSelectedListener, arg2=", " "+ arg2);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
mListView01.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selectedViewPos = arg2;
Log.d("TitlesSelectionDialog(),selectedViewPos= "," "+ selectedViewPos);
Toast.makeText(getApplicationContext(), "selectedViewPos= "+ selectedViewPos, Toast.LENGTH_LONG).show();
}
});
......
openDocAdapter的代码:
private class openDocAdapter extends SimpleAdapter
{
private Context _con;
private List _List;
private int _listviewId;
private String[] _keys;
private int[] _resValues;
public openDocAdapter(Context context, ArrayList<HashMap<String,Object>> List , int listviewId, String[] keys, int[] resValues )
{
super(context, List, listviewId, keys, resValues);
_con =context;
_List = List;
_listviewId = listviewId;
_keys = keys;
_resValues = resValues;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.main_open_content, null);
}
TextView title = (TextView) v.findViewById(R.id.title);
(...Similiar codes define textView, imageViewsd.)
return v;
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return super.getCount();
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return super.getItem(position);
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return super.getItemId(position);
}
}
Edit1:我发现article here关于布局上的焦点设置会导致可点击的工作或无法正常工作。所以,我在布局的xml文件中删除 (我不在这里编码)的行。然后在使用橙色焦点更改滚动列表列表时处理setOnItemSelectedListener()方法。但它仍然不符合我的期望。
答案 0 :(得分:3)
Provlem解决了!经过几次hous谷歌搜索/搜索和try_eror。如果你感兴趣,我想分享它。
问题的主要原因是:我使用 ScrollView 作为row.xml的基本布局(包含每个listview行的内容)。然后,我使用了 LinearLayout(Vertial)而不是它。 setOnItemClickedListener()方法现在正常工作。我对此没有任何想法会导致ListView无法点击。如果有人知道,请告诉我们,