简体   繁体   中英

How to get item at position in listview and send to another activity in android?

I have a class in which I have listview and what I want is when onclick item in listview get specific item and send it to another activity. how can I do that,Kindly help me out.

here is my listview Class:-

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

and here is my Adapter class

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

and here is my storage class:-

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

// here is the activty where you send your 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//
    }

Step 1 : Make storage class implement serializable

public class CDealAppDatastorage implements Serializable{

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

}

Step 3: in SecondActivity activity

CDealAppDatastorage = getIntent.getSerializableExtra("a");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM