現在、TaskListAdapter というアダプター クラスがあります。これは、現在 1 つの textView のみを処理しますが、後で 2 つの textView と 2 つの imageView を保持するように変更されます。コードは次のとおりです。
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
それで...何か助けて?前もって感謝します!前に言ったように... 私はこの分野に非常に慣れていないので、厳しすぎないようにしてください!