Android: 如何为对象编写自定义数组适配器
Android: How to write customize array adapter for objects
我正在尝试制作可用于对象“数据”的自定义数组适配器
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。
编辑--------------------------------
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) {
}
}
说明:您当前创建列表项的方式只会创建一个名为 "original" 的 listToDo 并在每次循环期间更改其中的值。同样在循环中,您再次添加相同的 original
对象,无论您在 user.txt
中有多少行。因此,让我们将 listToDo 实例创建移动到循环中,这样它将在每个循环中创建一个名为 original
的新 listTodo class 对象,设置一些值(选中和事物),然后添加到 data
列表
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) {
}
}
此外,我认为您的适配器 class 有点不对劲。
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
我正在尝试制作可用于对象“数据”的自定义数组适配器
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。
编辑--------------------------------
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) {
}
}
说明:您当前创建列表项的方式只会创建一个名为 "original" 的 listToDo 并在每次循环期间更改其中的值。同样在循环中,您再次添加相同的 original
对象,无论您在 user.txt
中有多少行。因此,让我们将 listToDo 实例创建移动到循环中,这样它将在每个循环中创建一个名为 original
的新 listTodo class 对象,设置一些值(选中和事物),然后添加到 data
列表
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) {
}
}
此外,我认为您的适配器 class 有点不对劲。
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