好吧所以我有一个数组适应列表视图(数组适应在另一个类中完成)..我只是让点击监听器工作的列表,但现在我想要设置它,以便当我点击一个项目,它拉来自点击项目的字符串,并将它们捎带在一个新活动的意图上..我想我应该使用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 my alertsadapter class ..
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;
}
}
答案 0 :(得分:0)
您需要使用onItemClick事件的参数
具有参数名称的完整更具可读性的参数枚举
(AdapterView<?> parent, View view, int pos, long id)
这意味着你有pos
参数指示适配器中的位置。
你要做的是:
pos
答案 1 :(得分:0)
周末有一个关于如何解决这个问题的顿悟,我终于找到了一个很好的解决我的应用程序..我知道它不是最佳的,因为我硬编码数字100到它但我的用途截至现在我知道我不会有那么多的清单项目..
我将这2位代码添加到我的alertsadapter类
int startzero = 0;
public static String[][] promomatrix = new String[6][100];
和
promomatrix [0] [startzero] = al.cityid; promomatrix [1] [startzero] = al.promoterid; promomatrix [2] [startzero] = al.promocontent; promomatrix [3] [startzero] = al.promotitle; promomatrix [4] [startzero] = al.locationid; promomatrix [5] [startzero] = al.cover;
startzero++;
然后转到我的homepageactivity类并将其添加到点击监听器
Intent intent = new Intent(
HomePageActivity.this,PromotionActivity.class);
intent.putExtra("listitemcity", AlertsAdapter.promomatrix[0][pos]);
intent.putExtra("listitempromoter", AlertsAdapter.promomatrix[1][pos]);
intent.putExtra("listitemcontent", AlertsAdapter.promomatrix[2][pos]);
intent.putExtra("listitemtitle", AlertsAdapter.promomatrix[3][pos]);
intent.putExtra("listitemlocation", AlertsAdapter.promomatrix[4][pos]);
intent.putExtra("listitemcover", AlertsAdapter.promomatrix[5][pos]);
finish();
startActivity(intent);
最后去了我的promotionactivity(我试图发送字符串)并添加了这个
Bundle extras = getIntent().getExtras();
if (extras == null){
return;
}
String listitemcity = extras.getString("listitemcity");
String listitempromoter = extras.getString("listitempromoter");
String listitemcontent = extras.getString("listitemcontent");
String listitemtitle = extras.getString("listitemtitle");
String listitemlocation = extras.getString("listitemlocation");
String listitemcover = extras.getString("listitemcover");
像魅力一样工作..我希望这有助于某人:)