How To Select Item In The List View Programmatically
I have an ArrayList 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
Solution 1:
set an onItemClickListener to the listview such that on click, you set a boolean flag that sets the checkbox in each row to selected. then call notifyDataSetChanged()
Solution 2:
Try this
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;
publicclassMainActivityextendsActivity {
ListView listView1;
ArrayList<ModelClass> modelClass = newArrayList<ModelClass>();
FruitSelectAdapter adapter;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
modelClass.add(newModelClass("Orange", true));
modelClass.add(newModelClass("Apple", false));
modelClass.add(newModelClass("Banana", false));
modelClass.add(newModelClass("Grapes", false));
listView1 = (ListView) findViewById(R.id.listView1);
adapter = newFruitSelectAdapter(MainActivity.this, modelClass);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(newOnItemClickListener() {
@OverridepublicvoidonItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stubif(modelClass.get(arg2).isSelected()){
modelClass.get(arg2).setSelected(false);
}else{
modelClass.get(arg2).setSelected(true);
}
adapter.notifyDataSetChanged();
}
});
}
}
activity_main.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.multiseekbar.MainActivity" ><ListViewandroid: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;
publicclassFruitSelectAdapterextendsBaseAdapter
{
private Activity activity;
private LayoutInflater inflater;
private ArrayList<ModelClass> modelClass=null;
publicFruitSelectAdapter(Activity activity, ArrayList<ModelClass> modelClass) {
this.activity = activity;
this.modelClass = modelClass;
}
@OverridepublicintgetCount() {
// TODO Auto-generated method stubreturn modelClass.size();
}
@Overridepublic Object getItem(int position) {
// TODO Auto-generated method stubreturn modelClass.get(position);
}
@OverridepubliclonggetItemId(int position) {
// TODO Auto-generated method stubreturn position;
}
@Overridepublic View getView(finalint position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stubfinal ViewHolder holder;
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
holder =newViewHolder();
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;
}
classViewHolder{
TextView txtFruitName;
CheckBox cbFruitSelectStatus;
LinearLayout linLayBackground;
}
}
row1.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:id="@+id/linLayBackground"android:layout_height="70dp"android:orientation="horizontal"
><TextViewandroid: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" /><CheckBoxandroid: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;
publicclassModelClass {
String fruitName;
boolean isSelected=false;
publicModelClass(String fruitName, boolean isSelected) {
this.fruitName = fruitName;
this.isSelected = isSelected;
}
publicStringgetFruitName() {
return fruitName;
}
publicvoidsetFruitName(String fruitName) {
this.fruitName = fruitName;
}
publicbooleanisSelected() {
return isSelected;
}
publicvoidsetSelected(boolean isSelected) {
this.isSelected = isSelected;
}
}
Post a Comment for "How To Select Item In The List View Programmatically"