如何在列表视图中选择项编程

编程入门 行业动态 更新时间:2024-10-19 16:37:39
本文介绍了如何在列表视图中选择项编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个的ArrayList<串GT;名单,其中包含从列表视图all_list一些项目。如何选择列表视图all_list这些项目编程通过检查的ArrayList<串GT;名单的内容?

I have an ArrayList<String> List which contains some items from the listview all_list. How can I select these items in the list view all_list programmatically by checking the ArrayList<String> List contents?

对于例如,的ListView all_list 包含[0]苹果                                    [1]的橙色                                     [2]香蕉

for e.g., listview all_list contains [0] apple [1] orange [2] banana

在的ArrayList&LT;串GT;名单,我有橙色的,所以我想被选中(高亮)上的列表视图all_list位置1项自动。

In ArrayList<String> List, I have orange so I want item on position 1 on the listview all_list to be selected (highlighted) automatically.

我一直在使用 all_list.setItemChecked()试过了,但不起任何作用,并关闭应用程序。我列出的适配器后执行操作。

I have tried using all_list.setItemChecked(), but it does nothing and shuts down the application. I am performing the operation after listing the adapter.

推荐答案

试试这个

MainActivity.java

package com.example.multiseekbar; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; public class MainActivity extends Activity { ListView listView1; ArrayList<ModelClass> modelClass = new ArrayList<ModelClass>(); FruitSelectAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); modelClass.add(new ModelClass("Orange", true)); modelClass.add(new ModelClass("Apple", false)); modelClass.add(new ModelClass("Banana", false)); modelClass.add(new ModelClass("Grapes", false)); listView1 = (ListView) findViewById(R.id.listView1); adapter = new FruitSelectAdapter(MainActivity.this, modelClass); listView1.setAdapter(adapter); listView1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub if(modelClass.get(arg2).isSelected()){ modelClass.get(arg2).setSelected(false); }else{ modelClass.get(arg2).setSelected(true); } adapter.notifyDataSetChanged(); } }); } }

activity_main.xml中

<RelativeLayout xmlns:android="schemas.android/apk/res/android" xmlns:tools="schemas.android/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.multiseekbar.MainActivity" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="fill_parent" > </ListView> </RelativeLayout>

FruitSelectAdapter.java

package com.example.multiseekbar; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.LinearLayout; import android.widget.TextView; public class FruitSelectAdapter extends BaseAdapter { private Activity activity; private LayoutInflater inflater; private ArrayList<ModelClass> modelClass=null; public FruitSelectAdapter(Activity activity, ArrayList<ModelClass> modelClass) { this.activity = activity; this.modelClass = modelClass; } @Override public int getCount() { // TODO Auto-generated method stub return modelClass.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return modelClass.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub final ViewHolder holder; if (inflater == null) inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (convertView == null) { holder =new ViewHolder(); convertView = inflater.inflate(R.layout.row1, null); holder.txtFruitName = (TextView)convertView.findViewById(R.id.txtFruitName); holder.cbFruitSelectStatus = (CheckBox)convertView.findViewById(R.id.cbFruitSelectStatus); holder.linLayBackground = (LinearLayout) convertView.findViewById(R.id.linLayBackground); convertView.setTag(holder); }else{ holder = (ViewHolder)convertView.getTag(); } holder.txtFruitName.setText(modelClass.get(position).getFruitName()); holder.cbFruitSelectStatus.setChecked(modelClass.get(position).isSelected()); if(modelClass.get(position).isSelected()){ holder.linLayBackground.setBackgroundColor(Color.parseColor("#80ccff")); }else{ holder.linLayBackground.setBackgroundColor(Color.parseColor("#FFFFFF")); } return convertView; } class ViewHolder{ TextView txtFruitName; CheckBox cbFruitSelectStatus; LinearLayout linLayBackground; } }

row1.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="schemas.android/apk/res/android" android:layout_width="match_parent" android:id="@+id/linLayBackground" android:layout_height="70dp" android:orientation="horizontal" > <TextView android:id="@+id/txtFruitName" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Fruit name" android:layout_weight="1" android:textSize="16sp" android:textColor="#000000" /> <CheckBox android:id="@+id/cbFruitSelectStatus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" /> </LinearLayout>

ModelClass.java

package com.example.multiseekbar; public class ModelClass { String fruitName; boolean isSelected=false; public ModelClass(String fruitName, boolean isSelected) { this.fruitName = fruitName; this.isSelected = isSelected; } public String getFruitName() { return fruitName; } public void setFruitName(String fruitName) { this.fruitName = fruitName; } public boolean isSelected() { return isSelected; } public void setSelected(boolean isSelected) { this.isSelected = isSelected; } }

更多推荐

如何在列表视图中选择项编程

本文发布于:2023-11-10 22:22:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1576618.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   如何在   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!