【问题标题】:Get click from button on every row in listview从列表视图中每一行的按钮获取点击
【发布时间】:2012-04-13 02:39:12
【问题描述】:

我生成了一个ListView,如下所示

我没有足够的代表来发布图片,你必须破译我的网址:image

上图中的蓝色行使用HashMap填充:

private void showListView(JSONArray rows, JSONArray totals){

final ListView list = (ListView) findViewById(R.id.historylist);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
String[] titles = new String[]{"recordID","date","description","num1","num2"};
SimpleAdapter mSchedule = null;

try{

    for(int i=0;i<rows.length();i++){
        map = new HashMap<String, String>();
        for(int n=0;n<allRows.getJSONArray(i).length();n++){
            map.put(titles[n], allRows.getJSONArray(i).getString(n));
        }
        mylist.add(map);
    }

    mSchedule = new SimpleAdapter(
        History.this,
        mylist,
        R.layout.history_row,
        titles,
        new int[] {R.id.textView0, R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4}
    );

    list.setAdapter(mSchedule);

}catch(Exception e){
    Log.e("Creating ListView", e.toString());
}

}

<LinearLayout >

<LinearLayout >
    <LinearLayout >
        <TextView (recordID) />
        <TextView (date) />
        <TextView (num1) />
        <TextView (num2) />
    </LinearLayout>
    <TextView (description) />
</LinearLayout>

<LinearLayout (When this one is clicked) >
    <ImageView />
</LinearLayout>

当点击上图中的绿色按钮时,我想获取蓝色行信息。

(日期,描述,num1,num2)

另外,如果您认为有更好的方法来填充 ListView,请告诉我。

【问题讨论】:

    标签: android listview hashmap


    【解决方案1】:

    您需要通过扩展BaseAdapter 来实现您自己的自定义适配器。在getView 方法中,您需要绑定到Button 的点击事件。我建议使用OnClickListener 的一个实例并使用AdapterView#getPositionForView。一旦你有了持有被点击按钮的视图的位置,你就可以访问你的数据。通过Adapter#getItem 或直接来自您的数据源。

    【讨论】:

    • 对不起,你完全失去了我。如果没有在代码中看到这一点,我不知道如何设置它。我尝试跟踪您的每个链接并将代码放在一起,但它看起来像科学怪人。
    • 有没有简单的方法可以做到这一点?我有一个线性布局,当点击线性布局时,我想获取行信息。
    • 我没有按照您的建议进行所有操作,但我确实扩展了 BaseAdapter 并发现了其功能的强大之处。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    相关资源
    最近更新 更多