Android:如何为对象编写自定义arrayadapter

时间:2015-01-01 01:33:13

标签: android object android-listview android-arrayadapter

我正在尝试制作可用于对象“数据”的自定义数组适配器

 public class theAdapter extends ArrayAdapter {
private ArrayList <listToDo> list;
public theAdapter(Context context, ArrayList<listToDo>list) {
    super(context, R.layout.my_layout, list);
    this.list = list;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater =  LayoutInflater.from(getContext());
    View view =  inflater.inflate(R.layout.my_layout,parent,false);
    listToDo activity = (listToDo) getItem(position);
    TextView theTextView = (TextView) view.findViewById(R.id.textView);
    theTextView.setText(activity.things);
    CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
    checkBox.setChecked(activity.checked);
    return view;
}

}

然而,这反复打印出同样的东西。 例如,如果ArrayList有1,2,3,4,5。 (这不是我的确切对象) 它打印出5,5,5,5,5。

EDIT ---------------------------------

        protected void dataRead (){
        File f = new File( Environment.getExternalStorageDirectory(),"user.txt");
        listToDo original = new listToDo ();
        try {
        FileInputStream fis = new FileInputStream(f);
        InputStreamReader temp = new InputStreamReader(fis);
        BufferedReader bis = new BufferedReader(temp);
        String activity;
        while ((activity = bis.readLine()) != null){
            String[] parts = activity.split("\t");
            original.things = parts[0];
            if (parts[1] == "1"){
                original.checked=true;
            }
            else{
                original.checked=false;
            }
            data.add(original);
            Log.v(TAG, original.things);
        }
            for (int i =0; i < data.size(); i++){
                Log.d(TAG,data.get(i).things);
            }
        bis.close();
    }
    catch  (Exception e) {
    }
     }

1 个答案:

答案 0 :(得分:0)

说明:您创建列表项的当前方式只会创建一个名为“original”的listToDo,并在每个循环期间更改其中的值。同样在循环中,您会再次添加相同的original对象,因为user.txt中有多行。所以,让我们在你的循环中移动listToDo实例,这样它就会在每个循环中创建一个名为original的新listTodo类对象,设置一些值(checked和things),然后添加到data List < / p>

protected void dataRead (){
    File f = new File( Environment.getExternalStorageDirectory(),"user.txt");
    try {
        FileInputStream fis = new FileInputStream(f);
        InputStreamReader temp = new InputStreamReader(fis);
        BufferedReader bis = new BufferedReader(temp);
        String activity;
        while ((activity = bis.readLine()) != null){
            listToDo original = new listToDo ();
            String[] parts = activity.split("\t");
            original.things = parts[0];
            if ("1".equals(parts[1])) {
                original.checked=true;
            }
            else{
                original.checked=false;
            }
            data.add(original);
            Log.v(TAG, original.things);
        }
        for (int i =0; i < data.size(); i++){
            Log.d(TAG,data.get(i).things);
        }
        bis.close();
    }
    catch  (Exception e) {
    }
}

另外,我认为您的适配器类有点偏。

public class theAdapter extends ArrayAdapter<listToDo> {

    public theAdapter(Context context, ArrayList<listToDo> list) {
        super(context, R.layout.my_layout, list);
    }

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

        listToDo activity = (listToDo) getItem(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.my_layout, parent, false);
        }

        TextView theTextView = (TextView) convertView.findViewById(R.id.textView);
        theTextView.setText(activity.things);
        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
        checkBox.setChecked(activity.checked);
        return convertView;
    }
}

以下是完整的教程以防万一: https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView