Following is my code.I use spring JSON restful web service to retrieve list of data and display at ADT.Display mulriple rows of data works fine ^_^.
My question is,how do I retrieve particular item row's value on user click button row?
| Id | Description | Button |
| 2 | iPhone 5 |(Submit) |
| 3 | Samsung Tab |(Submit) |
Lets say user click on "submit" button on 2nd row, so that system would know id is 3.
Hope someone advice
Regards
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);
}
}
}
the 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());
}
});
}
}
}
the main activity xml
<LinearLayout
android:clickable="true"
....>
<TextView
android:id="@+id/itemId"
....>
</TextView>
<Button
android:id="@+id/submitButton"
android:onClick="pickActions"
android:clickable="true"
..../>
</LinearLayout>
One simple way is to use setTag . You can set the item id on the button using setTag like this
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
}
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments