我目前有一个叫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
那么……有什么帮助吗?提前致谢!正如我之前所说......我对这个领域非常陌生,所以尽量不要太苛刻!