1
   public class ListViewActivity extends ListActivity implements OnClickListener {

ArrayList<String> datas;
DBhelper query;
//ImageView image;
CheckedTextView cap;
DBhelper db;
ArrayList<byte[]> image;
ArrayList<String> caption ;
ArrayList<String> description ;
ArrayList<String> rate ;

ByteArrayInputStream imageStream;
String d;
Bitmap theImage;
ItemsAdapter itemsAdapter;
Button order, menu;
byte[] temp_image;
ArrayList<String>   completedTasks;

Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);
    order = (Button) findViewById(R.id.order);
    order.setOnClickListener(this);

    db = new DBhelper(this);

    image = new ArrayList<byte[]>();
    caption = new ArrayList<String>();
    description = new ArrayList<String>();
    rate = new ArrayList<String>();
            // to retrieve the datas from database
    db.open();
    Cursor cursor = db.getEvents("data");
    if( cursor.moveToFirst())   {
        do{

            temp_image = cursor.getBlob(1); //retrieving the image
            //Log.e("image", temp_image.toString());// 
            String temp_caption = cursor.getString(2);//retrieving the data
            Log.e("name", temp_caption);
            String temp_description = cursor.getString(3);
            Log.e("desc", temp_description);
            String temp_rate = cursor.getString(4);
            Log.e("rate", temp_rate);
            //imageStream = new ByteArrayInputStream(temp_image);
            image.add(temp_image);
            caption.add(temp_caption);
            description.add(temp_description);
            rate.add(temp_rate);
        }while (cursor.moveToNext()) ;
        String[]   captionArray = (String[]) caption.toArray(
                new String[caption.size()]);// convert the 

                     arraylist to arrays.
                     // Adapter to set data to the list.
        itemsAdapter = new ItemsAdapter(
                ListViewActivity.this, R.layout.datalist_item,
                captionArray);
        setListAdapter(itemsAdapter);//set the adapter

        return ;
    }
    db.close();// database close
}

扩展基本适配器的适配器类。私有类 ItemsAdapter 扩展 BaseAdapter { String[] items;

    public ItemsAdapter(Context context, int textViewResourceId,
            String[] items) {
        this.items = items;
    } 


    public View getView(final int POSITION, View convertView,
            ViewGroup parent) {

        TextView tvDescription;
        TextView tvRate;

        View view = convertView;
        ImageView img;
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) 
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.datalist_item, null);

        } 
        img = (ImageView) view.findViewById(R.id.ivDatas);

        cap = (CheckedTextView) view.findViewById(R.id.list_content);
        tvDescription = (TextView) view.findViewById(R.id.tvdesc);
        tvRate = (TextView) view.findViewById(R.id.tvRate);
        theImage = BitmapFactory.decodeByteArray(image.get(POSITION), 
                    0, image.get(POSITION).length );
        img.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Log.e("ghhf", String.valueOf(imgid));
                //Bundle b = new Bundle();
                Log.e("tag", 
                     String.valueOf(image.get(POSITION)));
                i = new 
                      Intent(ListViewActivity.this,ImageActivity.class);
                //b.putParcelable("imagePath", 
                      image.get(POSITION));
                i.putExtra("imageP", image.get(POSITION));
                //i.putExtra(image.get(POSITION));

                startActivity(i);
            }
        });
        img.setImageBitmap(theImage);

        cap.setText(caption.get(POSITION));
        tvDescription.setText(description.get(POSITION));
        tvRate.setText(rate.get(POSITION));


        return view; 
    }


    public int getCount() {
        // TODO Auto-generated method stub
        return items.length;
    }


    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }


    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
}

这里我有一个问题,当checkedtextview被选中时,我必须将checkedtextview添加到arraylist。但是在这里,当我要选择两个checkedtextview 时,它只向最后选择的arraylist 添加一个值。

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    String selection = l.getItemAtPosition(position).toString();
    String s = caption.get(position);
    //ArrayList<Long> completedIds = new ArrayList<Long>();
    completedTasks = new ArrayList<String>();
    cap = (CheckedTextView) v.findViewById(R.id.list_content);

在这里,我必须获取选定的 textview 并添加到 arraylist,并将 arraylist 发送到下一个活动并显示它。

    if(!cap.isChecked())    {


        Log.d("checked", "true");

        completedTasks.add(s);
        cap.setChecked(true);
        Log.d("true", s);

        return ;    

    }

    else    {
        cap.setChecked(false);

        Log.d("unchecked", "true");
        //for(String str : caption )    {
        completedTasks.remove(s);
        Log.d("false", s);
        //}
        //itemsAdapter.notifyDataSetChanged();
        return ;


        //}
    }



}

在这里,我们必须显示选中的数组列表。

         public void onClick(View v) {
    for(String str : completedTasks)    {
        Log.d("selected", str);
    }

    }

}

4

1 回答 1

0

尝试移动这条线:completedTasks = new ArrayList<String>();在你的onListItemClick. 将其移至 onCreate 之类的。问题是每次您单击 listView 时,它都会重新制作同一个对象。这可能就是为什么您在数组中只看到一个字符串的原因。

我想你也有问题将 arrayList 放到另一个活动中。就我个人而言,我只是将其设为静态成员,但您应该比我更清楚地了解其他选项:

如何在 Android 上将对象从一个活动传递到另一个活动?

在android中的活动之间传递非原始类型的数据

于 2012-11-13T19:12:26.830 回答