1

我目前有一个叫TaskListAdapter的适配器类,目前只处理一个textView,但以后会改为容纳2个textViews和2个imageViews,代码是:

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class TaskListAdapter extends ArrayAdapter<TaskListItem> {
Context context;
int layoutResourceId;
TaskListItem data[] = null;

public TaskListAdapter(Context context, int layoutResourceId, TaskListItem[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    TaskListHolder holder = null;

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

        holder = new TaskListHolder();
        holder.txtTitle = (TextView)row.findViewById(R.id.txtDashCol1Name);

        row.setTag(holder);
    }
    else
    {
        holder = (TaskListHolder)row.getTag();
    }

    TaskListItem list_item = data[position];
    holder.txtTitle.setText(list_item.title);

    return row;
}

static class TaskListHolder
{
    TextView txtTitle;
}

}

它使用 TaskListItem 类,即:

public class TaskListItem {
    public String title;
    public TaskListItem() {
        super();
    }

    public TaskListItem(String title) {
        super();
        this.title = title;
    }

}

现在,在我处理 JSON 查询的主类中,它当前填充了一个列表。我对 Java 和 Android 开发非常陌生,所以请尝试解释所有答案!这是我从 URL 获取响应,然后解析响应的部分。

//It first gets the response from the http request.
            String response = new RequestTask(this).execute(MY_URL).get();
            //And then parses the JSONObject that is returned
            JSONObject results = new JSONObject(response);
            recordList = results.getJSONArray("records");
            int length = recordList.length();

            //Below, the text item at the top of the list which indicates the number of tasks total, is populated.
            ((TextView)findViewById(R.id.txtTotalListItems)).setText("Tasks (" + length + ")");

            //Then it loops around each entry in the JSONObject and adds anything with the label 'title' into an array
            List<String> listContents = new ArrayList<String>(length);
            for (int i=0; i < length; i++) {
                JSONObject item = recordList.getJSONObject(i);
                listContents.add(item.getString("title"));
            }

            myListView = (ListView) findViewById(R.id.ltPageTwoList);
            myListView.setAdapter(new ArrayAdapter<String>(TaskList.this, R.layout.list_item, R.id.txtTitle, listContents));

这被一个 try/catch 子句包围,如果我使用标准的 setAdapter(new ArrayAdapter... 东西,一切似乎都可以正常工作。我想要做的是有一个自定义适配器,用于替代列表颜色之类的东西,填充多个 textViews 和 imageViews。有什么想法吗?我尝试的第一件事是:

TaskListAdapter task = new TaskListAdapter(this, R.layout.task_list_layout, listContents);

但它抛出了错误:

The constructor TaskListAdapter(TaskList, int, List<String>) is undefined

那么……有什么帮助吗?提前致谢!正如我之前所说......我对这个领域非常陌生,所以尽量不要太苛刻!

4

1 回答 1

2

List 与对象数组不同。数组是一种更基本的类型,不能与列表互换使用。您之前没有遇到任何问题,因为使用 ArrayAdapter,有多个构造函数,其中一个被重载以使用对象列表。

ArrayAdapter(Context context, int textViewResourceId, List<T> objects)

在您定义的类中,您只定义了一个接受对象数组的构造函数,并且构造函数在 Java 中不会被继承,尽管您仍然可以通过 super() 调用访问它们。

要解决您的问题,只需将您的构造函数更改为

TaskListAdapter(Context context, int textViewResourceId, List<TaskListItem> objects)

并更改引用以反映您正在使用列表。即:

List<TaskListItem> data = new ArrayList <TaskListItem> ();
...
TaskListItem task_item = data.get(position);

如果您对 List 与数组、ArrayList 与 List 或继承感到困惑,我认为这些对于问题的范围来说太宽泛了,我建议您阅读一些基本的 java 教程。请参阅:http ://docs.oracle.com/javase/tutorial/java/index.html

编辑:所以在你解析 JSON 的类中,进行 add 调用

add(new TaskListItem(item.getString("title"))

而且,做我最初打算在Adapter课堂上做的事情,将所有对数组的引用更改为对列表的引用。

我突然想到,您之前在解析 json 的类中使用了一个 String 列表,并且您将更改该列表。

A quick note is by parameterizing a List you tell the Java compiler you're only going to be putting objects of a certain type in there. You parameterized the list with the type TaskListItem so it would not longer accept type String. What I told you to do changes your add call so it creates a new TaskListItem for each String and adds that, instead of attempting to add the plain String. If you're still having problems after this, post your full code and I'll try and help more.

于 2012-08-01T13:22:01.380 回答