ListView点击事件怎样获取点击项的ID或信息?该如何解决
ListView点击事件怎样获取点击项的ID或信息?
我临时用Eclipse Java编写Android软件,语言不熟练
我在主界面main.xml和item页面item.xml中 分别使用 ListView 和两个TextView显示,现在的问题是:
我需要在点击某项时,想取得该绑定的一些数据,比如
用item.put("id", model.getCustomServiceMsg_AutoID()); 绑定的id值,改肿么做呢?? 代码和界面代码如下
一:Activity代码:
二:main.xml 主界面代码:
三:Items项item.xml项代码:
------解决思路----------------------
你把List<HashMap<String,Object>> data改成全局变更 ,在点击 的时候 获取用data.get(arg2)获取到HashMap,再获取 key就是ID了
我临时用Eclipse Java编写Android软件,语言不熟练
我在主界面main.xml和item页面item.xml中 分别使用 ListView 和两个TextView显示,现在的问题是:
我需要在点击某项时,想取得该绑定的一些数据,比如
用item.put("id", model.getCustomServiceMsg_AutoID()); 绑定的id值,改肿么做呢?? 代码和界面代码如下
一:Activity代码:
package net.netkf.WXClient;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import net.netkf.Model.Model_WX_CustomServiceMsg;
import net.netkf.Model.News;
import net.netkf.Service.WXClientService;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class WXClientActivity extends Activity {
ListView lvm = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lvm = (ListView)this.findViewById(R.id.listviewMain);
try {
//List<Model_WX_CustomServiceMsg> list = WXClientService.GetXmlModellist();
List<Model_WX_CustomServiceMsg> list = WXClientService.GetJsonModellist();
List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
for(Model_WX_CustomServiceMsg model : list){
HashMap<String,Object> item = new HashMap<String,Object>();
item.put("id", model.getCustomServiceMsg_AutoID());
item.put("NicName", getResources().getString(R.string.nicname)+ model.getUserNicName());
item.put("Message",
getResources().getString(R.string.message)+ model.getUserMessage());//getResources()得到当前资源
data.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.lkfitems
,new String[]{"NicName","Message"},new int[]{R.id.tvtitle,R.id.tvmsg});
lvm.setAdapter(adapter);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lvm.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
switch(arg2){
case 0:
Toast.makeText(WXClientActivity.this, arg0.getId(), Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(WXClientActivity.this, arg1.showContextMenu()+"", Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(WXClientActivity.this, arg3+"", Toast.LENGTH_LONG).show();
break;
}
}});
}
}
二:main.xml 主界面代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listviewMain"
/>
</LinearLayout>
三:Items项item.xml项代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:id="@+id/tvtitle"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tvmsg"
/>
</LinearLayout>
------解决思路----------------------
你把List<HashMap<String,Object>> data改成全局变更 ,在点击 的时候 获取用data.get(arg2)获取到HashMap,再获取 key就是ID了