简体   繁体   English

Android ListView 如何获取每一行的值

[英]Android ListView how can I get the value of each individual row

I have a custom ListView working.我有一个自定义 ListView 工作。 What I would like to know is how can I check the value or get the text of the rows in the ListView in the GetView method .我想知道的是如何在 GetView 方法中检查 ListView 中的值或获取行的文本。 I essentially got a list of car names in the ListView such as Honda, Ford etc .我基本上在 ListView 中得到了一个汽车名称列表,例如 Honda、Ford 等。 When a user clicks the name of a car or car make then an image with that Car appears next to the text.当用户单击汽车或汽车制造商的名称时,文本旁边会出现带有该汽车的图像。 When the click event happens the car name/make is added to the String ArrayList selected_fields.add(selectedFromList) .当点击事件发生时,汽车名称/品牌被添加到 String ArrayList selected_fields.add(selectedFromList) 中 What I would like to do now is find a way where I can get the value of the rows inside the ListView so that I can see if they contain any value that is found in the selected_fields array.我现在想要做的是找到一种方法来获取 ListView 中行的值,以便我可以查看它们是否包含在 selected_fields 数组中找到的任何值。 As you know that things are recycled inside the ListView .如您所知,内容在 ListView 内被回收。 This is my code below这是我下面的代码

     @Override
    public View getView(final int positions, View convertView, ViewGroup parent) {
         final ViewHolder holder;
 // what I would like to do here is get the Value of the ListView Rows here
// I can then check if that value matches up with things inside the
// selected_fields array so that I can do something like
 // If convertView.Rows[value].contains(selected_genre) then show image . 


     if(convertView==null){
         inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         convertView = inflater.from(c).inflate(R.layout.car_names, null);

         holder.Car_Names=(TextView) convertView.findViewById(R.id.car_names);

     }



        binding.ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {


                String selectedFromList = ((String)binding.ListView.getItemAtPosition(position));
                  selected_fields.add(selectedFromList);

                holder.selected_genres = (ImageView)  view.findViewById(R.id.car_names);
                holder.car_names.setVisibility(View.VISIBLE);

            }

        });

        names= genres.toArray(new String[0]);
        holder.text1.setText(names[pos]);



  notifyDataSetChanged();

        return convertView;
    }


}

For example this snippet of code inside the Onclick method gets the value of the ListView row that is clicked .例如,Onclick 方法中的这段代码获取被单击的 ListView 行的值。 I now want to get the value of all ListView Rows outside the OnClick method and check if the ListView Rows contains a value inside the selected_fields array .我现在想获取 OnClick 方法之外的所有 ListView Rows 的值,并检查 ListView Rows 是否包含 selected_fields 数组内的值。

((String)binding.ListView.getItemAtPosition(position));
                      selected_fields.add(selectedFromList);

The easiest way to get the value associated with a row in a ListView is to call getItem() on its adapter.获取与ListView的行关联的值的最简单方法是在其适配器上调用getItem() If you use ArrayAdapter , this method returns the correct type due to the magic of generics so no carry is necessary.如果您使用ArrayAdapter ,由于泛型的魔力,此方法返回正确的类型,因此不需要进位。

Step 1: create an interface in your activity or fragment with a method that having single parameters of the vo that use used to pass to the adapter.步骤 1:使用具有用于传递给适配器的 vo 的单个参数的方法在您的活动或片段中创建一个接口。

step 2:第2步:

create the interface object and pass it to the adapter创建接口对象并将其传递给适配器

step 3:第 3 步:

create a click in adapter using the single items container.使用单项容器创建单击适配器。

step 4:第四步:

inside the click get the single item vo using position在点击内使用位置获取单个项目 vo

step 5:第 5 步:

make the interface method call using interface object .使用接口对象进行接口方法调用。 method name with the single item vo带有单项 vo 的方法名称

step 6:第 6 步:

inside the activity/fragment interface method use will get the data在活动/片段接口方法中使用将获取数据

step 7:第 7 步:

inside that method do what ever you wants to do.在那个方法里面做你想做的事。

FOR EXAMPLE例如

public interface ProcessClick {
     void getItem(Model model);
}






  public class MainActivity extends AppCompatActivity implements ProcessClick {
           private ArrayList<Model> model;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
     adapter = new Adapter(this, model);
            }

            @Override
            public void getItem(Model model) {

            }
        }

In Adapter在适配器中

private ProcessClick onclick;
 holder.container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Model model = models.get(position);
                onclick;.getItem(model);

            }
        });

in Your Adapter you can create Interface like this在您的适配器中,您可以创建这样的接口

 Listener mListener;
    interface Listener {
        void onClick(int position, Model mModel,mListArray);
    }

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

    ...
     holder.yourWidget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Model mModel = mListArray.get(position);
                mListener.onClick(position, mModel, mListArray);

            }
        });

        ...
    }

and in your activity you can impliment your interface like this在你的活动中,你可以像这样实现你的界面

public class yourActivity extends Activity implements yourAdapter.Listener
{
......

 @Override
public void onClick(int position, Model mModel, ArrayList<Model> mListArray) {
    do whatever you want to do with this perticular position
}
.....
}

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

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