我使用自定义列表视图使用hashmap和两个xml布局在listview中显示图像和文本信息,其中一个包含listview,另一个包含自定义行。并且下面的代码工作得很好。我应该如何向用户点击的行的位置干杯?
public class MainTopic extends Activity{
ListView mtopiclv;
LinearLayout header;
List<HashMap<String, String>> rowList;
String[] topicHeader ={
"What is farming ?",
"Raising animals ?",
"How to graz ?",
"sdsdsdsd sdsd",
"Control",
"Find product centers",
"Research and statistics",
"How to succeed in sdfsdfds ?"
};
String[] info = {
"Introduction, benefits",
"Seeds, planting and care",
"Requirements, , resources",
"Investing in farming",
"Practices and tools",
"machinery",
"books",
"Business tips"
};
int[] images = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3,
R.drawable.sample_4,
R.drawable.sample_5,
R.drawable.sample_6,
R.drawable.sample_7,
R.drawable.sample_8
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mtopic);
setUp();
hashmap();
}
private void hashmap() {
// TODO Auto-generated method stub
rowList = new ArrayList<HashMap<String, String>>();
//looping through all items
for(int i = 0; i<8; i++){
HashMap<String, String> infoList = new HashMap<String, String>();
infoList.put("topicHeader", topicHeader[i]);
infoList.put("info", info[i]);
infoList.put("imgs", Integer.toString(images[i]));
rowList.add(infoList);
//from
String[] from = {"topicHeader", "info", "imgs"};
int[] to = {R.id.clvHeader, R.id.clvsum, R.id.imgbtn};
//adapter
SimpleAdapter clvAdapter = new SimpleAdapter
(getApplicationContext(), rowList, R.layout.mtopic_custome_lv, from, to);
mtopiclv.setAdapter(clvAdapter);
}
}
private void setUp() {
// TODO Auto-generated method stub
mtopiclv = (ListView) findViewById(R.id.mtopiclv);
header = (LinearLayout) findViewById(R.id.header);
}
}
答案 0 :(得分:0)
尝试在listview上使用setOnItemClickListener
mtopiclv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View arg1, int position,
long arg3) {
Toast toast = Toast.makeText(getApplicationContext(), position+"", Toast.LENGTH_SHORT);
toast.show();
}
});