以下是我的代码。我使用spring JSON restful web服务来检索数据列表并在ADT显示。显示多行数据工作正常^ _ ^。
我的问题是,如何在用户点击按钮行检索特定项目行的值?
| Id | Description | Button |
| 2 | iPhone 5 |(Submit) |
| 3 | Samsung Tab |(Submit) |
让我们说用户点击第二行的“提交”按钮,这样系统就知道id是3。
希望有人建议
此致
public class MyMainActivity extends ListActivity{
@Override
protected void onCreate(){...}
@Override
protected void onStart(){
super.onStart();
new HttpRequestTask().execute();
}
public void pickActions(View v){
}
private class HttpRequestTask extends AsyncTask<Void,Void,ArrayList<ItemDetail>>{
protected ArrayList<ItemDetail> doInBackground(Void... params){
final String url = "http://10.0.2.2:8080/restdata";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
List<ItemDetail> resultList = Arrays.asList(restTemplate.getForObject(url, ItemDetail[].class));
ArrayList<ItemDetail> list = new ArrayList<ItemDetail>();
if(resultList.size()>0){
for(int i=0;i<resultList.size();i++){
ItemDetail itemDetail = resultList.get(i);
list.add(itemDetail);
}
}
return list;
}
@Override
protected void onPostExecute(ArrayList<ItemDetail> list) {
m_adapter = new TheArrayAdapter(MyMainActivity .this,list);
setListAdapter(m_adapter);
}
}
}
适配器
public class TheArrayAdapter extends ArrayAdapter<ItemDetail>{
private ArrayList<ItemDetail> objects;
private String currentSku;
public TheArrayAdapter(Context context,ArrayList<ItemDetail> objects){
super(context,R.layout.activity_main_list,objects);
the.objects = objects;
}
public View getView(int position,View convertView,ViewGroup parent){
View v = convertView;
//....//
ItemDetail i = objects.get(position);
if(i!=null){
TextView itemId= (TextView) v.findViewById(R.id.itemId)
Button btn = (Button) v.findViewById(R.id.submitButton)
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//new ListFruitActivity().pickActions(v);
//String s = currentsku;
v.getId();
System.out.println("selected: "+v.getId());
}
});
}
}
}
主要活动xml
<LinearLayout
android:clickable="true"
....>
<TextView
android:id="@+id/itemId"
....>
</TextView>
<Button
android:id="@+id/submitButton"
android:onClick="pickActions"
android:clickable="true"
..../>
</LinearLayout>
答案 0 :(得分:1)
一种简单的方法是使用 setTag 。您可以使用setTag在此按钮上设置项目ID
Button btn = (Button) v.findViewById(R.id.submitButton)
btn.setTag(id);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// retrieve the button item id
int itemId = (Integer)v.getTag();
// now you have the id, do your other stuff based not the id here
}
});
答案 1 :(得分:0)
你在getview meyhod中有位置参数,它会告诉你你要点击哪一行
public View getView(int position,View convertView,ViewGroup parent){
View v = convertView;
//....//
ItemDetail i = objects.get(position);
if(i!=null){
TextView itemId= (TextView) v.findViewById(R.id.itemId)
Button btn = (Button) v.findViewById(R.id.submitButton)
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("selected: "+position);
}
});
}