简体   繁体   English

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

[英]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.我有一个类,其中有 listview,我想要的是当 listview 中的 onclick 项目获取特定项目并将其发送到另一个活动时。 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) // 这是您发送意图的活动(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步骤 1:使存储类实现可序列化

public class CDealAppDatastorage implements Serializable{

Step 2:第 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第 3 步:在 SecondActivity 活动中

CDealAppDatastorage = getIntent.getSerializableExtra("a");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android如何将listview项目发送到另一个活动 - android How to send listview item to another activity 从 ListView 项目获取数据并发送到另一个活动 (Android) - Get data from a ListView item and send to another activity (Android) 如何在Android中将Listview项目数据(图像和文本)从一个活动发送到另一个活动 - How to send Listview item data (image & text) from one activity to another activity in android 如何在另一个活动中将字符串发送到listView? 在Android中 - how to send a string to listView in another activity ? in android 使用Android中的listview项目将图像从一个活动发送到另一个活动 - send image from one activity to another using listview item in android Android:如何获取ListView中单击的项目位置? - Android: how to get the clicked item position in the ListView? Android-如何获取ListView项目在绝对位置? - Android - How to get ListView item at absolute position? 如何将值从json列表视图中的选定项目发送到另一个活动? - How to send values from a selected item on a json listview to another activity? 如何将json列表视图上的选定项目发送到另一个活动? - How to send selected item on a json listview to another activity? Android:将listView项的位置传递给下一个活动 - Android: pass listView item position to next activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM