我正在做一个项目,我有一个列表视图,其中填充了来自HTTP Get请求的数据。现在我想在点击列表视图的某个项目时将其发送到我的服务器。问题是我不知道如何:
1)为每个不同的项目创建一个OnClick方法 2)获取我的第一个HTTP Get请求的ID,并将其分配给我的HTTP Post。
这是我的GET请求:
public class TestGet extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(params[0]);
try {
HttpResponse response = client.execute(get);
InputStream is = response.getEntity().getContent();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuilder str = new StringBuilder();
String chunk = null;
while ((chunk = br.readLine()) != null) {
str.append(chunk + "\n");
}
result = str.toString();
JSONArray jarray = new JSONArray(result);
for (int i = 0; i < jarray.length(); i++) {
JSONObject json_data = jarray.getJSONObject(i);
if(json_data.getString("Sent")=="true"){
DBQuestion qa = new DBQuestion();
qa.setName(json_data.getString("ParticipantName"));
qa.setQuestion(json_data.getString("QuestionText"));
qa.setId(json_data.getString("Id"));
qa.setScore(json_data.getString("Votes"));
questions.add(qa);
}
else i+=1;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
这是我的自定义适配器:
public class qa_adapter extends ArrayAdapter<DBQuestion> {
private Context context;
private int layoutId;
private List<DBQuestion> data;
public qa_adapter(Context context, int resource, List<DBQuestion> objects) {
super(context, resource, objects);
this.context=context;
layoutId=resource;
data = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View row = inflater.inflate(layoutId, parent,false);
TextView twName = (TextView) row.findViewById(R.id.tv_qa_row_name);
twName.setText(data.get(position).getName());
TextView twQuestion = (TextView) row.findViewById(R.id.tv_qa_row_question);
twQuestion.setText(data.get(position).getQuestion());
TextView twVotes = (TextView) row.findViewById(R.id.tv_qa_row_votes);
twVotes.setText("Votes: "+data.get(position).getScore());
return row;
}
我仍然没有发布我的HTTP帖子。
有什么建议吗?