我有一个列表视图,并希望根据每个列表视图项的数据发送请求。随后,我需要更新我发送的特定请求的textview。
这是我的arrayAdapter,我在其中填充listview并发送http请求
public class ListAdapter extends ArrayAdapter<lProperty> {
Context mContext;
List<lProperty> values = null;
public ListAdapter(Context mContext, List<lProperty> data) {
super(mContext, R.layout.list_row, data);
this.mContext = mContext;
this.values = data;
layoutInflater = LayoutInflater.from(mContext);
asyn = new AsyncHelper();
}
static class ViewHolder {
public ImageView image;
public TextView type;
public TextView info;
public RelativeLayout rl;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
holder = new ViewHolder();
if (convertView == null) {
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.list_row,
null);
holder.rl = (RelativeLayout) convertView.findViewById(R.id.row_layout);
holder.image = (ImageView) convertView.findViewById(R.id.images);
holder.type = (TextView) convertView.findViewById(R.id.type);
holder.info = (TextView) convertView.findViewById(R.id.info);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
Log.d(TAG, "List Size"+values.size());
holder.type.setText(values.get(position).type);
if(values.get(position).type.equals("Outsider")){
holder.info.setText(values.get(position).outside);
}
if(values.get(position).type.equals("Student")){
String url = "https:abc.com&code=" + values.get(position).code + "&type="+ values.get(position).type;
new HttpAsyncTask().execute(url);
holder.info.setText(marks);
}
if(values.get(position).type.equals("Employee")){
String url = "https:abc.com&code=" + values.get(position).code + "&type="+ values.get(position).type;
new HttpAsyncTask().execute(url);
holder.info.setText(marks);
}
if(values.get(position).type.equals("Other")){
String url = "https:abc.com&code=" + values.get(position).code + "&type="+ values.get(position).type;
new HttpAsyncTask().execute(url);
holder.info.setText(operator);
}
return convertView;
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute()
{
};
@Override
protected String doInBackground(String... urls) {
Log.d(TAG," URL"+urls[0]);
return GET(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
try {
JSONArray reader = new JSONArray(result);
if (!(reader.isNull(0))) {
holder.info.setText(reader.getJSONObject(0).getString("marks"));
Log.d(TAG, "Marks" + marks);
} else {
holder.info.setText("Not Found");
}
} catch (JSONException j){
j.printStackTrace();
}
}
}
public static String GET(String url){
InputStream inputStream = null;
String result = "";
try {
String jsonreply;
StringBuilder builder = new StringBuilder();
HttpClient client = asyn.getNewHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
jsonreply = builder.toString();
return jsonreply;
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return result;
}
static String TAG= "ListAdapter";
private LayoutInflater layoutInflater;
static AsyncHelper asyn;
ViewHolder holder;
String marks = null;
} 我也可以发送请求并接收数据,但是我无法就请求发送和更新相同的textview,任何建议建立关系
答案 0 :(得分:0)
尝试在更改文本值后添加notifyDataSetChanged()
:
if (!(reader.isNull(0))) {
holder.info.setText(reader.getJSONObject(0).getString("marks"));
notifyDataSetChanged(); //refresh your adapter
Log.d(TAG, "Marks" + marks);
}
在你的getView中,我觉得你最好这样做:
if(values.get(position).type.equals("Outsider")){
holder.info.setText(values.get(position).outside);
}
else if(values.get(position).type.equals("Student")){
String url = "https:abc.com&code=" + values.get(position).code + "&type="+ values.get(position).type;
new HttpAsyncTask().execute(url);
holder.info.setText(marks);
}
else if(values.get(position).type.equals("Employee")){
String url = "https:abc.com&code=" + values.get(position).code + "&type="+ values.get(position).type;
new HttpAsyncTask().execute(url);
holder.info.setText(marks);
}
else if(values.get(position).type.equals("Other")){
String url = "https:abc.com&code=" + values.get(position).code + "&type="+ values.get(position).type;
new HttpAsyncTask().execute(url);
holder.info.setText(operator);
}
将if
更改为else if
,它可以让您的应用提升效果。
答案 1 :(得分:0)
好的,现在我明白了,对不起,我没有看到你声明持有人在底部。 顺便说一句,如果你将holder声明为一个类字段,它将始终包含不同的视图,因为每次调用getView时它都会引用不同的视图。 您应该将持有者引用传递给AsynTask。
更新 - 添加代码 这样做
public HttpAsyncTask(ViewHolder view) {
myHolder = view;
}
@Override
protected void onPostExecute(String result) {
try {
JSONArray reader = new JSONArray(result);
if (!(reader.isNull(0))) {
myHolder.info.setText(reader.getJSONObject(0).getString("marks"));
Log.d(TAG, "Marks" + marks);
} else {
myHolder.info.setText("Not Found");
}
} catch (JSONException j){
j.printStackTrace();
}
}
Update2将持有者传递给AsyncTask
new HttpAsyncTask(holder).execute(url);