1

プログラミング初心者ですのでよろしくお願いします。画像を含むリストビューを作成できたので、それを変更してチェックボックスを追加できると考えました。ただし、この設定は機能しません。画像とチェックボックスの両方を含むリストビューを作成するようにコードを修正する方法について何か提案はありますか?

 import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ParseException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CheckboxList extends ListActivity{
        private LayoutInflater mInflater;
        private Vector<RowData> data;
        RowData rd;
        static CheckBox box0,box1,box2,box3;

        static final String[] title = new String[] {
                "Box 1", "Box 2",   

        "Box 3 ","Box 4"        };

        static final CheckBox[] boxes= new CheckBox[] {box0,box1,box2,box3};

        private Integer[] imgid = {
          R.drawable.magnum,R.drawable.mg,R.drawable.mk,
          R.drawable.mk46};

        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mInflater = (LayoutInflater) getSystemService(
        Activity.LAYOUT_INFLATER_SERVICE);
        data = new Vector<RowData>();
        for(int i=0;i<title.length;i++){

        try {
            rd = new RowData(i,title[i],boxes[i]);
            } catch (ParseException e) {
                e.printStackTrace();
           }
           data.add(rd);
        }
           CustomAdapter adapter = new CustomAdapter(this, R.layout.check_list,
                                             R.id.title, data);
           setListAdapter(adapter);
           getListView().setTextFilterEnabled(true);
        }
           public void onListItemClick(ListView parent, View v, int position,
                                                                        long id) {          

           Toast.makeText(getApplicationContext(), "You have selected "
                            +(position+1)+"th item",  Toast.LENGTH_SHORT).show();
        }
               private class RowData {
               protected int mId;
               protected String mTitle;
               protected CheckBox mCBox;
               RowData(int id,String title,CheckBox cbox){
               mId=id;
               mTitle = title;
               mCBox=cbox;
            }
               @Override
               public String toString() {
                       return mId+" "+mTitle+" "+mCBox;
               }
        }
          private class CustomAdapter extends ArrayAdapter<RowData> {

          public CustomAdapter(Context context, int resource,
                                int textViewResourceId, List<RowData> objects) {               

         super(context, resource, textViewResourceId, objects);
        }
              @Override
               public View getView(int position, View convertView, ViewGroup parent) {   

               ViewHolder holder = null;
               TextView title = null;
               CheckBox box = null;
               ImageView i11=null;
               RowData rowData= getItem(position);
               if(null == convertView){
                    convertView = mInflater.inflate(R.layout.check_list, null);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);
         }
                     holder = (ViewHolder) convertView.getTag();
                     title = holder.gettitle();
                     title.setText(rowData.mTitle);

                     box = holder.getcheckbox();
                     box.setChecked(false);                                                    

                     i11=holder.getImage();
                     i11.setImageResource(imgid[rowData.mId]);
                     return convertView;
        }
                    private class ViewHolder {
                    private View mRow;
                    private TextView title = null;
                    private CheckBox cbox = null;
                    private ImageView i11=null; 

                    public ViewHolder(View row) {
                    mRow = row;
         }
                 public TextView gettitle() {
                     if(null == title){
                         title = (TextView) mRow.findViewById(R.id.title);
                        }
                    return title;
                 }     

                 public CheckBox getcheckbox() {
                     if(null == cbox){
                          cbox = (CheckBox) mRow.findViewById(R.id.checkBox);
                            }
                   return cbox;
                 }
                public ImageView getImage() {
                     if(null == i11){
                          i11 = (ImageView) mRow.findViewById(R.id.img);
                                              }
                        return i11;
                }
             }
           } }
4

1 に答える 1

1

このリンクを見てください。役に立つかもしれません......

http://www.vogella.com/articles/AndroidListView/article.html

画像とチェックボックスを含む CustomListview の例

于 2012-05-17T03:53:47.520 に答える