Android:在listview中归档项目

时间:2011-08-09 18:44:56

标签: android listview archive

我有一个listview,其中我正在显示文本项目。我想显示一个复选框以及要执行的两种操作的文本。一种是当用户点击它应该带到另一个活动的列表项而另一个是用户可以选择列表项的复选框并且可以将项移动到某些组。与我们在gmail收件箱中的操作类似。我该怎么做 ?有什么帮助吗?

我在这里附上我的代码。 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>

用于显示列表项布局的xml文件

<?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>

和我活动的java代码

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;
    }

}

}

1 个答案:

答案 0 :(得分:0)

在xml中添加CheckBox,其中<ListView >或xml用作项目布局。然后在getView()中沿TextView实例化CheckBox。实施listview.setOnItemClickListener()并根据点击的项目(位置)开始活动[逻辑问题]对于检查项目的分组,您可以使用getCheckedItemPositions()。我希望这能激发您的想法。