如何从自定义适配器数据的ListView? [英] How to get data from custom adapter in ListView?

查看:134
本文介绍了如何从自定义适配器数据的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:

我创造了我的ListView自定义适配器,我得到的信息指出,我给该适配器为了让他履行我的ListView名单。他正确地做一切。现在我想实现一个 OnItemClickListener 并从中获取数据。但在名单上有4 TextViews ...

如何检索数据

我已经知道如何获得ID和位置,因为我举杯显示出来。

非常感谢您的帮助!

下面是自定义适配器:

 公共类NextRdvAdapter扩展ArrayAdapter< NextRdv> {
私人列表< NextRdv> listNextRdv =新的ArrayList< NextRdv>();
私人上下文的背景下;公共NextRdvAdapter(列表< NextRdv> nextRdvList,上下文CTX){
    超(CTX,R.layout.list_next_rdv,nextRdvList);
    this.listNextRdv = nextRdvList;
    this.context = CTX;
}公共查看getView(INT位置,查看convertView,父母的ViewGroup){    如果(convertView == NULL){//这是一个新的观点,我们膨胀的新布局
        LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_next_rdv,父母,假);
        } //现在我们可以填写了正确的价值观布局    TextView的tvTimeStart =(TextView中)convertView.findViewById(R.id.next_time_start);
    TextView的tvTimeEnd =(TextView中)convertView.findViewById(R.id.next_time_end);
    TextView的tvEnterprise =(TextView中)convertView.findViewById(R.id.next_enterprise_name);
    TextView的tvAddress =(TextView中)convertView.findViewById(R.id.next_address);
    NextRdv RDV = listNextRdv.get(位置);    tvTimeStart.setText(rdv.getStartTime());
    tvTimeEnd.setText(rdv.getEndTime());
    tvEnterprise.setText(rdv.getEnterprise());
    tvAddress.setText(rdv.getAddress());
    返回convertView;
    }
}

下面是我的片段是posess ListView的:

 公共类HomeFragment扩展片段实现OnItemClickListener {浮密度;//所有你需要这个RDV
私人的ListView lvThisRdv;
私人列表< NextRdv> listThisRdv =新的ArrayList< NextRdv>();
私人NextRdvAdapter ThisRdvAdapter;
私人NextRdvListBouchon thisBouchon;//你需要的一切关于下一RDV
私人的ListView lvNextRdv;
私人列表< NextRdv> listNextRdv =新的ArrayList< NextRdv>();
私人NextRdvAdapter nextRdvAdapter;
私人NextRdvListBouchon BOUCHON;公共HomeFragment(){}@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){    查看rootView = inflater.inflate(R.layout.fragment_home,集装箱,FALSE);
    lvThisRdv =(ListView控件)rootView.findViewById(R.id.listView_this_RDV);
    thisBouchon =新NextRdvListBouchon();
    listThisRdv = thisBouchon.getBouchonInc();
    ThisRdvAdapter =新NextRdvAdapter(listThisRdv,getActivity());
    lvThisRdv.setAdapter(ThisRdvAdapter);    //显示下一次的会议列表
    lvNextRdv =(ListView控件)rootView.findViewById(R.id.listView_next_RDV);    //获取测试数据
    BOUCHON =新NextRdvListBouchon();
    listNextRdv = bouchon.getBouchon();    nextRdvAdapter =新NextRdvAdapter(listNextRdv,getActivity());
    lvNextRdv.setAdapter(nextRdvAdapter);    lvNextRdv.setOnItemClickListener(新OnItemClickListener(){          @覆盖
          公共无效onItemClick(适配器视图<>母公司,观景,
            INT位置,长的id){              //
              吐司面包= Toast.makeText(getActivity(),波什:+位置+/ ID:+ ID,Toast.LENGTH_SHORT);
              toast.show();
          }    });    返回rootView;
}@覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,
        长ID){
}
}


解决方案

onItemClick 您可以使用适配器视图参数

  NextRdv项目=(NextRdv)parent.getItemAtPosition(位置)

here is my problem :

I created a custom adapter for my ListView and I get information out of a List that I give to that adapter in order to let him fulfill my ListView. He does everything correctly. Now I'd like to implement an OnItemClickListener and to get the data from it. But in that list I have 4 TextViews ...

How can I retrieve the data ?

I already know how to get the id and position since I display them in a Toast.

Thanks a lot for your help !

Here is the custom Adapter :

public class NextRdvAdapter extends ArrayAdapter<NextRdv> {
private List<NextRdv> listNextRdv = new ArrayList<NextRdv>(); 
private Context context; 

public NextRdvAdapter(List<NextRdv> nextRdvList, Context ctx) {
    super(ctx, R.layout.list_next_rdv, nextRdvList);
    this.listNextRdv = nextRdvList; 
    this.context = ctx;
} 

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

    if (convertView == null) { // This a new view we inflate the new layout 
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        convertView = inflater.inflate(R.layout.list_next_rdv, parent, false); 
        } // Now we can fill the layout with the right values 

    TextView tvTimeStart = (TextView) convertView.findViewById(R.id.next_time_start); 
    TextView tvTimeEnd = (TextView) convertView.findViewById(R.id.next_time_end); 
    TextView tvEnterprise = (TextView) convertView.findViewById(R.id.next_enterprise_name); 
    TextView tvAddress = (TextView) convertView.findViewById(R.id.next_address); 
    NextRdv rdv = listNextRdv.get(position); 

    tvTimeStart.setText(rdv.getStartTime());
    tvTimeEnd.setText(rdv.getEndTime());
    tvEnterprise.setText(rdv.getEnterprise());
    tvAddress.setText(rdv.getAddress());


    return convertView; 
    }
}

Here is my Fragment that posess the ListView :

public class HomeFragment extends Fragment implements OnItemClickListener{

float density;

//Everything you need about this rdv
private ListView lvThisRdv;
private List<NextRdv> listThisRdv = new ArrayList<NextRdv>();
private NextRdvAdapter ThisRdvAdapter;  
private NextRdvListBouchon thisBouchon;

//Everything you need about next rdv
private ListView lvNextRdv;
private List<NextRdv> listNextRdv = new ArrayList<NextRdv>();
private NextRdvAdapter nextRdvAdapter;  
private NextRdvListBouchon bouchon;

public HomeFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);


    lvThisRdv = (ListView)rootView.findViewById(R.id.listView_this_RDV);
    thisBouchon = new NextRdvListBouchon();
    listThisRdv = thisBouchon.getBouchonInc();
    ThisRdvAdapter = new NextRdvAdapter(listThisRdv, getActivity());
    lvThisRdv.setAdapter(ThisRdvAdapter);

    //Displaying the list of the next meetings
    lvNextRdv = (ListView)rootView.findViewById(R.id.listView_next_RDV);

    //Get test data
    bouchon = new NextRdvListBouchon();
    listNextRdv = bouchon.getBouchon();

    nextRdvAdapter = new NextRdvAdapter(listNextRdv, getActivity());
    lvNextRdv.setAdapter(nextRdvAdapter);

    lvNextRdv.setOnItemClickListener(new OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

              //
              Toast toast = Toast.makeText(getActivity(), "Pos : " +position+ " / id : " + id, Toast.LENGTH_SHORT);
              toast.show();
          }

    });

    return rootView;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
}
}

解决方案

inside onItemClick you can use the AdapterView argument,

NextRdv item = (NextRdv) parent.getItemAtPosition(position)

这篇关于如何从自定义适配器数据的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆