好吧,所以我有一個數組適應listview(數組適配是在另一個類中完成)..我只是得到了點擊監聽器爲列表工作,但現在我想要設置它,以便當我點擊一個項目時,它從被點擊的項目中抽取字符串,並將它們附加到新活動的意圖上。我想我應該使用intent.putextra但是我不知道如何拉正確的字符串對應於我點擊..我的代碼是下面的項..即時通訊只是失去了誠實如何從一個適合列表項的字符串中抽取一個點擊列表項的列表
//Initialize the ListView
lstTest = (ListView)findViewById(R.id.lstText);
//Initialize the ArrayList
alrts = new ArrayList<Alerts>();
//Initialize the array adapter notice with the listitems.xml layout
arrayAdapter = new AlertsAdapter(this, R.layout.listitems,alrts);
//Set the above adapter as the adapter for the list
lstTest.setAdapter(arrayAdapter);
//Set the click listener for the list
lstTest.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView adapterView, View view, int item, long arg3) {
Intent intent = new Intent(
HomePageActivity.this,
PromotionActivity.class
);
finish();
startActivity(intent);
}
});
我的警示類..
public class Alerts {
public String cityid;
public String promoterid;
public String promoshortcontent;
public String promocontent;
public String promotitle;
public String locationid;
public String cover;
@Override
public String toString() {
return "City: " +cityid+ " Promoter: " +promoterid+ "Short Promotion: " +promoshortcontent+ "Promotion: " +promocontent+ "Title: " +promotitle+ "Location: " +locationid+ "Cover: " +cover+ "$";
}
}
anddddd我alertsadapter類..
public class AlertsAdapter extends ArrayAdapter<Alerts> {
int resource;
String response;
Context context;
//Initialize adapter
public AlertsAdapter(Context context, int resource, List<Alerts> items) {
super(context, resource, items);
this.resource=resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout alertView;
//Get the current alert object
Alerts al = getItem(position);
//Inflate the view
if(convertView==null)
{
alertView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi;
vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resource, alertView, true);
}
else
{
alertView = (LinearLayout) convertView;
}
//Get the text boxes from the listitem.xml file
TextView textPromo =(TextView)alertView.findViewById(R.id.txtPromo);
TextView textPromoter =(TextView)alertView.findViewById(R.id.txtPromoter);
TextView textLocation =(TextView)alertView.findViewById(R.id.txtLocation);
//Assign the appropriate data from our alert object above
textPromo.setText(al.promocontent);
textPromoter.setText(al.promoterid);
textLocation.setText(al.locationid);
return alertView;
}
}
說實話我只編程了大約一個星期了..一切都完成了,通過在谷歌和使用本網站的研究教程..我知道如何做你的第三個子彈所以我一直試圖研究前兩個無濟於事。任何方式,你可以寫一些示例代碼或告訴我如何讀取適配器中某個位置的值的教程? – dootcher 2010-08-04 19:35:31
ArrayAdapter有一個'getItem'方法,您可以使用它來獲取位置處的項目。這可能是一個字符串,所以你也完成了第2步。它會是這樣的:如果我瞭解該代碼的權利,然後這將適用於一個工作的字符串myItem = HomePageActivity.this.arrayAdapter.getItem(item)' – Pentium10 2010-08-04 20:16:19
k listview是由列表項填充的,每個列表項只有一個相應的字符串..但是我忽略了提及每個列表項都有多個對應的字符串..檢查我的代碼上面,我剛剛添加..它的我的Alerts類和我的AlertsAdapter類這擴展了ArrayAdapter,所以我想這使得它更復雜..對不起: -/ –
dootcher
2010-08-04 20:31:14