gpt4 book ai didi

android - OnItemClickListener 从模型中获取数据

转载 作者:太空宇宙 更新时间:2023-11-03 11:52:48 25 4
gpt4 key购买 nike

我是 Android 开发的新手,我正在尝试构建一个 ListView,它使用 gson 从 Web 服务获取数据。我有一个模型类、一个列表类、一个适配器类和一个 Activity 类。

该列表工作正常并且它获得了数据,现在我想将 OnItemClickListener 集成到它并将数据传递给第二个 Activity 。我想获取项目 ID (DistrictId) 并将其传递给下一个 Activity(listView) 而不是行 ID。如果有人能向我展示光线,那就太好了……因为文档理解起来不太清楚,而且我是新手。

下面是我的代码。

模型类

package com.sample.myapp;

public class DistrictModel {
private String id;
private String districtName;

public String getDistrictId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDistrictName(){
return districtName;
}
public void setDistrictEN(String districtName){
this.districtName = districtName;
}
}

列表类

public class DistrictList {
private List<DistrictModel> districts;

public List<DistrictModel> getDistricts(){
return districts;
}
public void setDistrictList(List<DistrictModel> districts){
this.districts = districts;
}
}

适配器类

public class DistrictAdapter extends ArrayAdapter<DistrictModel>{
int resource;
String response;
Context context;
private LayoutInflater dInflater;

public DistrictAdapter(Context context, int resource, List<DistrictModel> objects) {
super(context, resource, objects);
this.resource = resource;
dInflater = LayoutInflater.from(context);
}

static class ViewHolder {
TextView title;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
//Get the current location object
DistrictModel lm = (DistrictModel) getItem(position);

//Inflate the view
if(convertView==null)
{
convertView = dInflater.inflate(R.layout.item_district, null);
holder = new ViewHolder();
holder.title = (TextView) convertView
.findViewById(R.id.district_name);

convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}

holder.title.setText(lm.getDistrictName());

return convertView;
}
}

Activity 类

public class DistrictListActivity extends Activity{
LocationManager lm;

ArrayList<DistrictModel> districtArray = null;
DistrictAdapter districtAdapter;
DistrictList list;

ListView lv;

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.districtlist_layout);

lv = (ListView) findViewById(R.id.list_district);

districtArray = new ArrayList<DistrictModel>();
districtAdapter = new DistrictAdapter(DistrictListActivity.this, R.layout.item_district, districtArray);

lv.setTextFilterEnabled(true);
lv.setAdapter(districtAdapter);

try {
new DistrictSync().execute("http://aws.something.com/service");
} catch(Exception e) {}

lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View convertView, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(DistrictListActivity.this);
adb.setTitle("LVSelectedItemExample");
adb.setMessage("Selected Item is = "+(lv.getItemIdAtPosition(position)));
adb.setPositiveButton("Ok", null);
adb.show();

}
}); **//i'd like to get the DistrictId from the json data.**
}



private class DistrictSync extends AsyncTask<String, Integer, DistrictList> {

protected DistrictList doInBackground(String... urls) {
DistrictList list = null;
int count = urls.length;

for (int i = 0; i < count; i++) {
try {
// ntar diganti service
RestClient client = new RestClient(urls[i]);

try {
client.Execute(RequestMethod.GET);
} catch (Exception e) {
e.printStackTrace();
}

String json = client.getResponse();

list = new Gson().fromJson(json, DistrictList.class);

//
} catch(Exception e) {}
}
return list;
}

protected void onProgressUpdate(Integer... progress) {

}

protected void onPostExecute(DistrictList dislist) {

for(DistrictModel lm : dislist.getDistricts())
{
districtArray.add(lm);
}
districtAdapter.notifyDataSetChanged();
}

}
}

出于测试目的,现在我单击该行,它将显示行 ID,因此我知道 onclick 监听器有效,但我只希望它获取 DistrictId,以便我可以使用它传递到下一个 Activity 。

非常感谢。

最佳答案

(出乎我的意料)试试这个:

((DistrictModel)lv.getAdapter().getItem(position)).getDistrictId();

关于android - OnItemClickListener 从模型中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7731495/

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