0

我使用自定义列表视图在我的列表视图中使用 hashmap 和两个 xml 布局显示图像和文本信息,其中一个包含列表视图,另一个包含自定义行。下面的代码工作得很好。我应该如何敬酒用户点击的行的位置?

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);

}

}

4

1 回答 1

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();

    }

});

于 2013-08-25T02:57:50.833 回答