在Android中的活动中的两个列表视图之间移动项目?

时间:2015-03-07 18:29:43

标签: android android-activity android-listview

我想实现待办事项列表,在活动中获取两个列表视图,listview1和listview2垂直堆叠,listview1将允许添加项目,每个项目都有一个标签和复选框,当点击项目的复选框时,我需要要将项目移动到listview2,listview2中的项目应显示其选中的复选框,因此listview2保存选中的项目。如果在List2中未选中该项,则应返回到list1。我怎样才能实现这种行为?以下代码是我到目前为止所尝试的。

public class ListItem{
  String desc;
  boolean isSelected;
  //..Getters & Setters ...
}
///////////////////////////////////////////////////

public class ListItemAdapter extends ArrayAdapter<ListItem>  {
Context context;
List<ListItem> items = null;
List<ListItem> checkedItems = new ArrayList<ListItem>();
int layoutResourceId;

public ListItemAdapter(Context context, int layoutResourceId, List<ListItem> items) {
    super(context, layoutResourceId, items);
    this.context = context;
    this.layoutResourceId = layoutResourceId;
    this.items = items;
}

static class ViewHolder{
    protected TextView txtDesc;
    protected CheckBox checkbox;
}

@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
    View row = convertView;
 //   row.setOnClickListener(null); 

    if(row == null) {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.txtDesc = (TextView)row.findViewById(R.id.txtTitle);
        viewHolder.checkbox = (CheckBox)row.findViewById(R.id.checkBox1);
        row.setTag(viewHolder);

        viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                        ListItem currentItem = (ListItem) viewHolder.checkbox.getTag();
                        currentItem.setSelected(buttonView.isChecked());
                        if(currentItem.isSelected()){
                          //add item to checkedItems list
                           //remove item from uncheckedlist
                        }
                        notifyDataSetChanged();
                    }
                });
    }else{
        row = convertView;
        ((ViewHolder) row.getTag()).checkbox.setTag(items.get(position));
    }

    ListItem item = items.get(position);
    ViewHolder holder = (ViewHolder) row.getTag();
    holder.txtDesc.setText(item.getDesc());
    holder.checkbox.setSelected(item.isSelected());

    return row;
}

}

///////////////////////////////////////////////////

public class DetailsActivity extends Activity {

float HINT_FONT_SIZE = 10.0F;
float TEXT_FONT_SIZE = 12.0F;

List<ListItem> uncheckedItems = new ArrayList<ListItem>();
List<ListItem>checkedItems = new ArrayList<ListItem>();
//  List<ListItem> items = null;

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);



    final EditText editText = (EditText)findViewById(R.id.editText);

    final ListView listView = (ListView) findViewById(R.id.listView);
    final ListView listView2 = (ListView) findViewById(R.id.listView2);
    final ListItemAdapter adapter = new ListItemAdapter(this,R.layout.details_item_row, uncheckedItems);
   // checkedItems = ListManager.getInstance().getCheckedItems();
    final ListItemAdapter adapter2 = new ListItemAdapter(this,R.layout.details_item_row, checkedItems);
    listView.setAdapter(adapter);
    listView2.setAdapter(adapter2);

    ........
    }

2 个答案:

答案 0 :(得分:0)

我这样做了:
在listview项目中:item.setId(youId);
然后在onClickListener {onClick(View v){v.getId();}}

答案 1 :(得分:0)

如果其他人正在寻找答案,这是我的解决方案。

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="7dp"
>
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="14dp"
android:textStyle="normal"
android:layout_marginRight="25dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>

public class AlphabetListDemo extends Activity {

//String of alphabets //
List<ListItem> alphabets = new ArrayList<ListItem>();
List<ListItem> prods = new ArrayList<ListItem>();

ListView L1, L2;
myAdapter myadp;
myAdapter2 myadp2;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alphabet_list_demo);

L1 = (ListView)findViewById(R.id.list1);
L2 = (ListView)findViewById(R.id.list2);

myadp = new myAdapter(this,alphabets);
myadp2 = new myAdapter2(this,prods);
L1.setAdapter(myadp);
L2.setAdapter(myadp2);

L1.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
        //add to Other List & update
        prods.add(alphabets.get(arg2));
        L2.setAdapter(myadp2);

        //remove from current List & update
        alphabets.remove(arg2);
        L1.setAdapter(myadp);
    }
});

   L2.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
        //add to Other List & update
        alphabets.add(prods.get(arg2));
        L1.setAdapter(myadp);

        //remove from current List & update
        prods.remove(arg2);
        L2.setAdapter(myadp2);
    }
});

final EditText textField = (EditText) findViewById(R.id.editText);
Button addBtn = (Button) findViewById(R.id.addBtn);
addBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        if(textField.getText().length()>0){
            alphabets.add(new ListItem(textField.getText().toString(),false));
            L1.setAdapter(myadp);
        }
    }
});

}


class myAdapter extends ArrayAdapter<ListItem>
{
TextView label;
CheckBox checkBox;
View row;
public myAdapter(Context context,List<ListItem>list)
{
    super(context, android.R.layout.simple_list_item_1, list);
}

public View getView(final int position, View convertView, ViewGroup parent)
{
    try{
        LayoutInflater inflater=getLayoutInflater();
        row = inflater.inflate(R.layout.simple_list_item_1, parent, false);
        label = (TextView)row.findViewById(R.id.item_title);
        checkBox= (CheckBox)row.findViewById(R.id.checkbox1);
        label.setText(alphabets.get(position).description);
        label.setTextColor(Color.BLACK);
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                prods.add(alphabets.get(position));
                L2.setAdapter(myadp2);

                //remove from current List & update
                alphabets.remove(position);
                L1.setAdapter(myadp);
            }
        });

    }catch(Exception e){

    }
    return row;
}
}
 // adapter for second list.....
class myAdapter2 extends ArrayAdapter<ListItem>
{
TextView label;
CheckBox checkBox;
View row;
public myAdapter2(Context context,List<ListItem>list)
{
    super(context, android.R.layout.simple_list_item_1, list);
}

public View getView(final int position, View convertView, ViewGroup parent)
{
    try{
        LayoutInflater inflater=getLayoutInflater();
        row = inflater.inflate(R.layout.simple_list_item_1, parent, false);
        label = (TextView)row.findViewById(R.id.item_title);
        checkBox= (CheckBox)row.findViewById(R.id.checkbox1);
        label.setText(prods.get(position).description);
        label.setTextColor(Color.BLUE);
        checkBox.setChecked(true);

        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //add to Other List & update
                alphabets.add(prods.get(position));
                L1.setAdapter(myadp);

                //remove from current List & update
                prods.remove(position);
                L2.setAdapter(myadp2);
            }
        });
    }catch(Exception e){

    }
    return row;
}
}

class ListItem{
public boolean isSelected;
public String description;

ListItem(String description, boolean isSelected){
    this.description = description;
    this.isSelected=isSelected;
}
}