gpt4 book ai didi

android - android中ListView中的图像和按钮

转载 作者:行者123 更新时间:2023-11-30 00:00:42 26 4
gpt4 key购买 nike

我有一个listView,可以使用AsyncTask从MySql获取数据,但我的问题是当我在ListView中添加一些按钮和图像时。我添加了一些按钮,但我可以使用它,但图像不会显示。

这是我的代码...

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

studentList = new ArrayList<HashMap<String, String>>();

new Loadstudent().execute();

ListView lv = getListView();



lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Intent i = new Intent(getApplicationContext(), Maindetail.class);
String sidlist = ((TextView) view.findViewById(R.id.tid)).getText().toString();
String namelist = ((TextView) view.findViewById(R.id.sname)).getText().toString();
i.putExtra(TAG_ID, sidlist);
i.putExtra(TAG_NAME, namelist);
startActivity(i);
}
});

}

class Loadstudent extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading students. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}

protected String doInBackground(String... args) {

List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_student, "GET", params);

Log.d("ALL student: ", json.toString());

try {

int success = json.getInt(TAG_SUCCESS);

if(success == 1) {
student = json.getJSONArray(TAG_STUDENT);

for (int i=0; i<student.length(); i++) {
JSONObject c = student.getJSONObject(i);

sid = c.getString(TAG_ID);
name = c.getString(TAG_NAME);
photo = c.getString(TAG_PHOTO);

HashMap<String, String> map = new HashMap<String, String>();

map.put(TAG_ID, sid);
map.put(TAG_NAME, name);
map.put(TAG_PHOTO, photo);

studentList.add(map);
}
}else {

}

} catch (JSONException e) {
e.printStackTrace();
}
return null;
}

protected void onPostExecute(String file_url) {
pDialog.dismiss();

runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, studentList,
R.layout.list_main, new String[] {
TAG_PHOTO,TAG_ID, TAG_NAME},
new int [] { R.id.imageviewlist, R.id.tid, R.id.sname });
setListAdapter(adapter);

}
});
}

}

我从网上学习了lazyadapter,但是在将其与代码结合时遇到了问题。有任何可以解决我的问题的教程或链接都会有很大的帮助。提前致谢。

最佳答案

嘿尝试下面的方法-

       lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

Intent i = new Intent(getApplicationContext(), Maindetail.class);
HashMap<String, String> data = studentList.get(position);
String sidlist = data.get(TAG_ID);
String namelist = data.get(TAG_NAME);
i.putExtra(TAG_ID, sidlist);
i.putExtra(TAG_NAME, namelist);
startActivity(i);
}
});

关于android - android中ListView中的图像和按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25056710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com