1

好的,所以我有一个适应数组的列表视图(数组适应是在另一个类中完成的).. 我刚刚让单击侦听器为列表工作,但现在我想设置它,以便当我单击一个项目时,它会从单击项目并将它们搭载在新活动的意图上..我认为我应该使用 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+ "$";
}

}

和dddd我的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;
}

}

4

2 回答 2

0

您需要使用 onItemClick 事件的参数

具有参数名称的更易读的参数枚举是

(AdapterView<?> parent, View view, int pos, long id)

这意味着您有pos指示适配器中位置的参数。

你要做的是:

  • 跳转到pos适配器
  • 从适配器读出值
  • 使用 putExtra 注册 Intent
于 2010-08-04T18:21:30.157 回答
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++;

然后转到我的主页活动类并将其添加到点击侦听器

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);

最后去了我的促销活动(我试图发送字符串)并添加了这个

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");

像魅力一样工作..我希望这可以帮助某人:)

于 2010-08-09T07:49:35.983 回答