我有一个ArrayAdapter,其getView使用数据设置listview中的项目。当我点击列表视图中的一行时,我想开始另一个活动(我可以这样做),但是将该活动的一些数据发送到另一个视图中。
如何在onItemClick方法中访问getView中的数据?
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rotarowlayout, parent,
false);
TextView startTime = (TextView) rowView
.findViewById(R.id.rowstarttime);
TextView duration = (TextView) rowView
.findViewById(R.id.rowduration);
TextView status = (TextView) rowView.findViewById(R.id.rowstatus);
TextView name = (TextView) rowView.findViewById(R.id.rowclientname);
TextView actualTimeIn = (TextView) rowView.findViewById(R.id.rowactualtimein);
TextView actualTimeOut = (TextView) rowView.findViewById(R.id.rowactualtimeout);
String record = list.get(position).toString();
String[] itemsInRecord = record.split(",");
Log.e(TAG, "itemin record = " + itemsInRecord.length);
String[] recordItem = new String[itemsInRecord.length];
for (int x = 0; x < itemsInRecord.length; x++) {
recordItem[x] = itemsInRecord[x];
}
//set date above listview
textViewDate.setText("Rota For " + recordItem[0].substring(1, 11));
String withoutBraket = recordItem[0].substring(11);
String withoutSecs = withoutBraket.substring(0, 6);
Log.e(TAG, "recordItem = " + recordItem[2]);
String _statusString = recordItem[2].toString().trim();
if(_statusString.equalsIgnoreCase("Completed")){
statusField = "c";
}else if(_statusString.equalsIgnoreCase("NCR")){
statusField = "NCR";
}else if(_statusString.equalsIgnoreCase("Waiting")){
statusField = "W";
}
Log.e(TAG, "statusField = " + statusField);
startTime.setText(withoutSecs+" " );
duration.setText( recordItem[1]+" mins ");
status.setText( statusField);
name.setText( " " + recordItem[3] + recordItem[4]);
callID = recordItem[5];
// needName = recordItem[6];
actualTimeIn.setText("This call was completed at " + recordItem[6] + " - ");
if( ! recordItem[7].toString().trim().equalsIgnoreCase( "]")){
String stripBracket = recordItem[7].toString().trim();
Log.e(TAG, "stripBracket = " + stripBracket);
String stripBracket2 = stripBracket.substring(0,5);
Log.e(TAG, "SB after split = " + stripBracket2 );
actualTimeOut.setText(stripBracket2);
}
return rowView;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.e(TAG, "inside onItemClick");
Intent i = new Intent(GetRota.this, someOtherActivity.class);
i.putsomedata;
startActivity(i);
}
答案 0 :(得分:3)
通过使用setTag()
和getTag()
方法,您可以在onItemClick()
中从适配器获取数据。像这样
在适配器类
中@Override
public View getView(int position, View convertView, ViewGroup parent) {
rowView.setTag(yourdata_object);
return rowView;
}
在项目上点击
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
YourDataObject item = (YourDataObject ) view.getTag();
Intent i = new Intent(GetRota.this, someOtherActivity.class);
i.putsomedata;
startActivity(i);
}
答案 1 :(得分:0)
试试这个。
您将获得onItemClick方法中单击项的位置以及单击列表中的视图。因此,您可以将标记设置为在getView中查看,在onitem中,您可以获取该视图标记并传递给intent。
rowView.setTag(YOURDATA); // in getView
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.e(TAG, "inside onItemClick");
Intent i = new Intent(GetRota.this, someOtherActivity.class);
(yourData) = view.getTag(); // get the tag value and set to putextra
i.putsomedata;
startActivity(i);
}