2012-04-13 63 views
0

我产生了ListView所看到如下从按钮获得点击每一行中的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,请让我知道。

回答

0

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

+0

对不起,但你完全失去了我。没有在代码中看到这一点,我不知道如何设置它。我试着跟随你的每一个链接并把代码放在一起,但它看起来像弗兰肯斯坦。 – gatzkerob 2012-04-15 05:33:34

+0

有没有简单的方法来做到这一点?我在一行中有一个线性布局,当点击线性布局时,我想获得行信息。 – gatzkerob 2012-04-15 23:38:49

+0

我没有完成你所建议的所有内容,但是我扩展了'BaseAdapter'并发现了它的特性的缺点。谢谢。 – gatzkerob 2012-05-01 03:48:39