如何在listview中获取项目并发送到android中的另一个活动?

时间:2016-04-04 05:35:58

标签: android listview

我有一个课程,其中我有listview,我想要的是当listview中的onclick项目获取特定项目并将其发送到另一个活动。我怎么能这样做,请帮助我。

这是我的listview类: -

m_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // how can I send item from specific position to another activity here
     }
});

这是我的Adapter类

    public class ApplistAdapter extends BaseAdapter {
    private Context m_Context;
    private LayoutInflater inflater;
    private ArrayList<CDealAppDatastorage> s_oDataset;
    public ApplistAdapter(Context m_Context,ArrayList<CDealAppDatastorage> mDataList) {
        this.m_Context = m_Context;
        s_oDataset = mDataList;
    }
    @Override
    public int getCount() {
        return s_oDataset.size();
    }

    @Override
    public Object getItem(int position) {
        return s_oDataset.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) m_Context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.deallisting_card_view, null);
        TextView header = (TextView) convertView.findViewById(R.id.headingText);
        TextView subHeader = (TextView) convertView.findViewById(R.id.subHeaderText);
        TextView dummyText = (TextView) convertView.findViewById(R.id.subHeadingText);
        ImageView logoImage = (ImageView) convertView.findViewById(R.id.appImage);

        // getting movie data for the row
        CDealAppDatastorage m = s_oDataset.get(position);
        // title
        header.setText(m.getM_szHeaderText());
        subHeader.setText(m.getM_szsubHeaderText());
        logoImage.setImageResource(m.getM_n_Image());

        return convertView;
    }
}

这是我的存储类: -

    public class CDealAppDatastorage {
    public String m_szHeaderText, m_szSubHeaderText;
    public int m_n_Image;

    public String getM_szHeaderText() {
        return m_szHeaderText;
    }

    public void setM_szHeaderText(String m_szHeaderText) {
        this.m_szHeaderText = m_szHeaderText;
    }

    public String getM_szsubHeaderText() {
        return m_szSubHeaderText;
    }

    public void setM_szsubHeaderText(String m_szsubHeaderText) {
        this.m_szSubHeaderText = m_szsubHeaderText;
    }

    public int getM_n_Image() {
        return m_n_Image;
    }

    public void setM_n_Image(int m_n_Image) {
        this.m_n_Image = m_n_Image;
    }
 }

2 个答案:

答案 0 :(得分:1)

   //in your on create motod do it 
  listView=findViewById(R.id.grid_View);
    UnlockAdapter adapter= new UnlockAdapter(Home.this,values);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(Home.this, "you pressed on"+position, Toast.LENGTH_SHORT).show();
            String str_pos = String.valueOf(position);
            Intent intent = new Intent(getApplicationContext(),Sub_Details.class);
            intent.putExtra("myKey", str_pos);
            startActivity(intent);
        }
    });

//这里是你发送意图的活动(Sub_Detail)

 Bundle extras = getIntent().getExtras();
    String tmp = extras.getString("myKey");
    Toast.makeText(this, "new Activity: "+tmp, Toast.LENGTH_SHORT).show();
    int tmps= Integer.parseInt(tmp);
  // now do whatever you want on that position
 
 
    if (tmps==0){
       /* Toast.makeText(this, "my second:"+tmps, Toast.LENGTH_SHORT).show();*/
        
    }
    if (tmps==1){
       //do something//
    }
    if (tmps==2){
        //do somehting//
    }

答案 1 :(得分:0)

步骤1:使存储类实现可序列化

public class CDealAppDatastorage implements Serializable{

第2步:

    m_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CDealAppDatastorage a = list.get(position);
Intent myIntent = new Intent(v.getContext(), SecondActivity.class);
myIntent.putExtra("a", a);

startActivity(myIntent);

}

第3步: 在SecondActivity活动中

CDealAppDatastorage = getIntent.getSerializableExtra("a");