1

さて、配列適応リストビューがあります(配列適応は別のクラスで行われます)。クリックリスナーがリストに対して機能するようになりましたが、アイテムをクリックしたときに文字列をプルするように設定したいと思います。アイテムをクリックして、新しいアクティビティの意図にピギーバックします。intent.putextraを使用することになっていると思いますが、クリックしたアイテムに対応する正しい文字列を取得する方法がわかりません。私のコードは以下のとおりです。im正直に言うと単に負けた

//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;
}

}

4

2 に答える 2

0

onItemClickイベントのパラメーターを使用する必要があります

パラメータ名を持つ完全で読みやすいパラメータ列挙型は

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

posこれは、アダプター内の位置を示すパラメーターがあることを意味します。

あなたがしなければならないことは:

  • posアダプターにジャンプします
  • アダプターから値を読み取ります
  • putExtraを使用してインテントにサインアップします
于 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 に答える