从ListView获取列表项

时间:2018-11-11 13:16:32

标签: android android-studio

我有兴趣在单击列表视图中的列表项时获得对该列表项的对象引用。我的自定义项目XML上的每个项目都默认包含一个hidden的Button和TextView。点击某个项目后,应将其visibility更改为visible
这就是我在集成图形编辑器中完成的意思。

enter image description here

应该成为这个

enter image description here

这是我对AdapterView的自定义getView


@Override
public View getView(int position, View v, ViewGroup viewGroup){

    if(v==null){
        LayoutInflater li= LayoutInflater.from(getContext());
        v= li.inflate(R.layout.custom_list_item, null);
    }

    Student s= getItem(position);
    TextView name= v.findViewById(R.id.txt_name);
    TextView surname= v.findViewById(R.id.txt_surname);
    TextView id= v.findViewById(R.id.txt_id);

    //following methods come from my Student class
    if(s != null){
        name.setText(s.getName());
        surname.setText(s.getSurname());
        id.setText(s.getId());
    }
    return v;
}

这是我(想)在“活动”中执行的操作,但目前还行不通。


ListView list= findViewById(R.id.advanced_list);
    final StudentAdapter adapter= new StudentAdapter(
            this,
            R.layout.custom_list_item,
            getStudents()
    );

    list.setAdapter(adapter);


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

            TextView tel= findViewById(R.id.txt_tel);
            Button delete= findViewById(R.id.btn_delete);

            tel.setVisibility(View.VISIBLE);
            delete.setVisibility(View.VISIBLE);
        }
    });

2 个答案:

答案 0 :(得分:1)

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

        TextView tel= findViewById(R.id.txt_tel);
        Button delete= findViewById(R.id.btn_delete);

        tel.setVisibility(View.VISIBLE);
        delete.setVisibility(View.VISIBLE);
    }
});

您正在尝试在View布局中查找Activity,但它们位于您作为参数的View中。您可以这样修复它:

...
TextView tel= view.findViewById(R.id.txt_tel);
Button delete= view.findViewById(R.id.btn_delete);
...

答案 1 :(得分:1)

您可以在listView行本身中切换可见性。我首先要说明一个在您的代码中发现的问题:

  

TextView tel = findViewById(R.id.txt_tel);

它应该为TextView tel= view.findViewById(R.id.txt_tel);,因为TextView电话不是您的主布局的一部分,而是listView的布局。 以下是有关如何处理可见性的两种解释。

基本答案:public View getView方法中,只需为要单击以使其他项目可见的任何项目设置onClickListeneronTouchListener。 要切换TextView的可见性,只需执行textView.setVisibility(View.VISIBLE);并将其放在onClickListener中。所有这些代码都应该在您的ListView适配器中。

详细说明: 这是代码

public class MyAdapter extends ArrayAdapter<MyClass> {
     private Context mContext;
     Activity activity;
     private List<MyClass> class = new ArrayList<>();

     public LogAdapter(Activity a, Context context, ArrayList<MyClass> list) {
         super(context, 0, list);
         mContext = context;
         activity = a;
         class= list;
     }
     @NonNull
     @Override
     public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
         View listItem = convertView;
         if (listItem == null){
             //If the row is empty, inflate it with your elements found in the 'mylistview' layout file.
             listItem = LayoutInflater.from(mContext).inflate(R.layout.mylistview, parent, false);
         }
         //These names and views are made up. You can change them to suit your needs for which items should become visible on a certain items click.
         Button changeVisibility = listItem.findViewById(R.id.change);
         TextView name = listItem.findViewById(R.id.name);
         TextView surname = listItem.findViewById(R.id.surname);

         changeVisibility.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(final View view) {
                 name.setVisibility(View.VISIBLE);
                 surname.setVisibility(View.VISIBLE);
             }
         }
    }
}

名称,例如MyAdapter和类MyClass只是示例,您需要制作它们。完成后,您的Adapter类应与上面的类似。您还可以更改按钮和TextView名称,这仅取决于您使用listItem进行膨胀的布局文件。

如果您的代码仍然有问题,请查看this link,以获取有关如何从头开始制作自定义listView的说明。 希望这会有所帮助!