クリックされたリスト項目のリストビューに適合した配列から文字列を取得する方法

StackOverflow https://stackoverflow.com/questions/3408352

質問

OK、それで私はリストビューに適応した配列を持っています(配列の適応は別のクラスで行われます)。リストに対してクリックリスナーが機能するようになりましたが、アイテムをクリックすると、クリックされたアイテムから文字列を取得し、新しいアクティビティへのインテントに便乗するように設定したいと思います。私は、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+ "$";
}

}

そして私のアラートアダプタークラス..

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

}

役に立ちましたか?

解決 2

この問題を解決する方法について週末にひらめきを持っていたと私は最終的に私のアプリのために周りに良い仕事を見つけた私はハード、それに番号100をコード化されたので..私はイマイチ最適なそれを知っているが、今、私のように自分の用途に私は今まで、多くのリスト項目を持つ習慣を知っている..

私はalertsadapterクラスにコードのこれら2ビットを追加

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

の魅力のように働いた..私はこれが誰かの役に立てば幸い:)

他のヒント

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

パラメータ名を含む完全に読みやすいパラメータ列挙型は次のとおりです。

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

それはあなたが持っていることを意味します pos アダプター内の位置を示すパラメーター。

あなたがしなければならないことは次のとおりです:

  • にジャンプします pos アダプターの中で
  • アダプターから値を読み出す
  • putExtra を使用してインテントにサインアップします
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top