我試圖開發一個Android應用程序,並且我目前被某些東西卡住了。如何從ListView中的單個項目中烘烤特定的文本ID
我使用自定義列表視圖看起來如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:paddingBottom="10dp" >
<TextView
android:id="@+id/name"
style="@style/..."/>
<TextView
android:id="@+id/number"
android:layout_below="@+id/..."
style="@style/..." />
<TextView
android:id="@+id/id"
android:layout_below="@+id/..."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible" />
</RelativeLayout>
的ID在這裏從我的SQLite數據庫,在那裏我通過我的聯繫表查詢得到他們的文字。
現在我想要點擊列表視圖中顯示的項目時發生的事情。點擊已註冊,但現在我想要一個Toast,它只顯示了正在傳入ID的數字(每行的主鍵)。
我不能擁有arrayindex +1,因爲我沒有通過id命令查詢。
這裏是我的onItemClick:
contactList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(Contacts.this,"My unique rowID should be presented here.",
Toast.LENGTH_SHORT).show();
}
});
我送的唯一ID對TextView的,因爲我相信它會讓點擊具體項目時可以更輕鬆地提取信息。
我的基本適配器:
public class ListViewCustomAdapter extends BaseAdapter {
public ArrayList<String> name;
public ArrayList<String> number;
public ArrayList<String> id;
public Activity context;
public LayoutInflater inflater;
public ListViewCustomAdapter(Activity context,
ArrayList<String> name, ArrayList<String> number, ArrayList<String> id) {
// TODO Auto-generated constructor stub
super();
this.context = context;
this.name = name;
this.number = number;
this.id = id;
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return name.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int id) {
return id;
}
public class ViewHolder {
TextView txtViewName;
TextView txtViewNumber;
TextView txtViewId;
}
public View getView(int position, View convertview, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertview == null) {
holder = new ViewHolder();
convertview = inflater.inflate(R.layout.listview_row, null);
holder.txtViewName = (TextView) convertview
.findViewById(R.id.name);
holder.txtViewNumber = (TextView) convertview
.findViewById(R.id.number);
holder.txtViewId = (TextView) convertview
.findViewById(R.id.id);
convertview.setTag(holder);
} else {
holder = (ViewHolder) convertview.getTag();
}
holder.txtViewName.setText(name.get(position));
holder.txtViewNumber.setText(number.get(position));
holder.txtViewId.setText(id.get(position));
return convertview;
}
}
任何幫助,將不勝感激。
是什麼問題? –
是你的每個listview元素的XML? –
是的,它是詹姆斯。而我的主鍵目前正在傳遞到@ + id/id。所以第一個項目會得到1,第二個會得到2,第三個會得到3等。如果我通過contact_id命令asc – Alex07