所有
感谢您的帮助。我的问题是如何将arrayadapter处理传递给它的listarray,为什么在我将新的arraylist分配给vlist之后它不起作用(请参考注释行之间的代码)?我尝试使用vlist = new ArrayList();而不是vlist.clear();但它只是不起作用。
import .....
public class MainActivity extends Activity {
Button load;
ListView content;
Context context;
ArrayAdapter<String> adapter;
List<String> vlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
load = (Button) findViewById(R.id.load);
content = (ListView) findViewById(R.id.content);
txtJson = (TextView) findViewById(R.id.txtjson);
context = this;
vlist = new ArrayList<String>();
load.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DataLoader dLdr = new DataLoader();
try {
dLdr.execute(new URI("http://someweb:3000/videolist"));
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
});
adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, vlist);
content.setAdapter(adapter);
}
private class DataLoader extends AsyncTask<URI, Integer, List<String>> {
ProgressDialog wait = new ProgressDialog(context);
@Override
protected void onPostExecute(List<String> result) {
super.onPostExecute(result);
/*******************************************************************/
vlist.clear();
for (int i = 0; i < result.size(); i++) {
vlist.add(result.get(i));
}
adapter.notifyDataSetChanged();
/*******************************************************************/
wait.dismiss();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
wait.setTitle("Loading...");
wait.show();
}
@Override
protected List<String> doInBackground(URI... params) {
HttpClient client = new DefaultHttpClient();
HttpGet getter = new HttpGet(params[0]);
List<String> results = new ArrayList<String>();
try {
HttpResponse response = client.execute(getter);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
InputStreamReader isRdr = new InputStreamReader(is);
BufferedReader bfRdr = new BufferedReader(isRdr);
StringBuilder strBldr = new StringBuilder();
String line = null;
while ((line = bfRdr.readLine()) != null) {
strBldr.append(line);
}
String result = strBldr.toString();
JSONObject jObj = new JSONObject(result);
JSONArray jArray = jObj.getJSONArray("list");
results = new ArrayList<String>();
for (int i = 0; i < jArray.length(); i++) {
results.add((String) jArray.getString(i));
}
return results;
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
return null;
}
}
}
答案 0 :(得分:0)
您可能想要创建自己的适配器,您可以在其中设置列表。
查看http://www.piwai.info/android-adapter-good-practices/了解详情。
答案 1 :(得分:0)
退房 ArrayAdaptor constructor code
您可以看到约束器调用
Arrays.asList(objects)
由它自己创建一个新的ArrayList see function实例。
意味着更改vlist不会更改ArrayAdaptor正在使用的Arraylist。
最简单的解决方法是致电
adapter.clear()
adapter.addAll(result)
adapter.notifyDataSetChanged();