0

I'm having a listview in which I'm displaying the text items. I want to display a checkbox along with the text for two type of actions to be performed. one is when the user click on the list item it should take to another activity and another is the user can select the checkbox of the list item and can move the items to some groups. As similar to that we do in the gmail inbox. how can i do it ? Any help ?

Here I attach my code. keywords.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/keywordlayout"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="@drawable/background_gradient"
 android:orientation="vertical"
 >
 <LinearLayout
    android:id="@+id/category_title_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="#fff"
    >
    <TextView
        android:id="@+id/category_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="#000"
        android:textStyle="italic"
        android:typeface="sans"
        android:gravity="center"
        />
 </LinearLayout>
 <ListView
    android:id="@+id/keywordslist"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_gradient"
    >

 </ListView>
</LinearLayout>

The xml file used for displaying list items layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_display"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<CheckBox
    android:id="@+id/chk_box"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:visibility="invisible"
>
</CheckBox>
<TextView 
    android:id="@+id/key_id"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    >
</TextView>
<TextView 
    android:id="@+id/key_name"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp"
    android:textColor="#FFF">
</TextView>
</LinearLayout>

and my java code for the activity

package com.sample.epiphron;


public class KeywordsList extends Activity {

TextView category_title;
ListView keywords_list ;
String categoryid, categoryname;
private ArrayList<KeywordDetails> listItems = new ArrayList<KeywordDetails>(); 

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.keywords);
    Bundle extras = getIntent().getExtras();
    categoryid = extras.getString("category_id");
    categoryname = extras.getString("category_name");

    category_title = (TextView) findViewById(R.id.category_title);
    category_title.setText(""+categoryname+"- Keywords");
    keywords_list = (ListView) findViewById(R.id.keywordslist);
    drawList(LoginScreen.user_credential, categoryid);
    //Toast.makeText(this, chkbx.toString(), Toast.LENGTH_SHORT).show();
    keywords_list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            TextView txtv1 = (TextView) arg1.findViewById(R.id.key_id);
            Toast.makeText(KeywordsList.this, txtv1.getText(), Toast.LENGTH_LONG).show();
        }
    });

}

public void drawList(String userid, String cat_id){
    WebServiceCall res = new WebServiceCall();
    SoapObject result = res.fetchKeyword(userid, cat_id);

    ArrayList<String> al = new ArrayList<String>();
    for (int i = 0; i<result.getPropertyCount(); i++){
        SoapObject obj = (SoapObject) result.getProperty(i);
        al.add((String) obj.getProperty("keywordname"));
        KeywordDetails object = new KeywordDetails(Integer.parseInt(obj.getProperty("keywordid").toString()), obj.getProperty("keywordname").toString());
        listItems.add(object);
    }
    for ( int j = 0; j < result.getPropertyCount(); j++ ) {

    }
    keywords_list.setAdapter(new ListItemsAdapter(listItems));
}


public class KeywordDetails{
    private int keyword_id;
    private String keyword_name;

    public KeywordDetails(int key_id, String key_name){

        this.keyword_id = key_id;
        this.keyword_name = key_name;
    }
    public int getId(){
        return this.keyword_id;
    }
    public String getName(){
        return this.keyword_name;
    }

    //...
}
private class ListItemsAdapter extends ArrayAdapter<KeywordDetails> {
    ArrayList<KeywordDetails> obj = null;
    KeywordDetails keyworddtls = null;
    public ListItemsAdapter(ArrayList<KeywordDetails> items) {
        super(KeywordsList.this, android.R.layout.simple_list_item_1, items);
        this.obj = items;
    }

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

        LayoutInflater inflater = getLayoutInflater();
        convertView = inflater.inflate(R.layout.keyword_list_item, null);
        final TextView text1 = (TextView) convertView.findViewById(R.id.key_id);
        TextView text2 = (TextView) convertView.findViewById(R.id.key_name);
        CheckBox chkbx = (CheckBox) convertView.findViewById(R.id.chk_box);
        keyworddtls = obj.get(position);

        text1.setText(Integer.toString(keyworddtls.getId()));
        text2.setText(keyworddtls.getName());
        chkbx.setVisibility(View.VISIBLE);                                  
        return convertView;
    }

}

}

EboMike
  • 76,846
  • 14
  • 164
  • 167
Ravi
  • 1
  • 2

1 Answers1

0

Add the CheckBox in the xml where is your <ListView > or in the xml which you use as item layout. Then in the getView() instantiate the CheckBox along the TextView. Implement listview.setOnItemClickListener() and based on which item was clicked (position) you start the activity [matter of logic] For the grouped of check items you can use getCheckedItemPositions(). I hope this will give you fire up idea.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • Hi Nikola, Thanks for your response. If I keep the checkbox in the item layout xml the checkbox is displayed along with the list item but the listitem item click even is not working. what will be the reason. – Ravi Aug 10 '11 at 07:19
  • Could you share your implementation of the list item click listener? – Nikola Despotoski Aug 10 '11 at 11:15