由setOnItemClickListener
ID參數返回的值總是0(零)不管我點擊返回的值位置始終的ListView的itemId始終爲0
但正確位置的值而我自己也嘗試adapterView.getItemIdAtPosition(i)
和仍然點擊列表中的每個項的值是零
public class ListFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{
ListView onlineList;
FloatingActionButton onlineListFab;
private static final int mLoaderId = 1;
ListAdapter listAdapter;
public ListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_list, container, false);
initilize(v);
return v;
}
private void initilize(View v) {
onlineList = (ListView)v.findViewById(R.id.onlineList);
onlineListFab = (FloatingActionButton)v.findViewById(R.id.onlineListFab);
onlineListFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),AddItem.class));
}
});
listAdapter = new ListAdapter(getActivity(),null);
onlineList.setAdapter(listAdapter);
onlineList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent details = new Intent(getActivity(),AddItem.class);
details.setData(Uri.withAppendedPath(TableName.mContentUri,String.valueOf(l)));
Toast.makeText(getActivity(),l+"",Toast.LENGTH_SHORT).show();
//startActivity(details);
}
});
loadItems();
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
switch (id){
case mLoaderId:
return new CursorLoader(getContext(), TableName.mContentUri,null,null,null,null);
}
return null;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
listAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
listAdapter.swapCursor(null);
}
private void loadItems() {
if (getActivity().getSupportLoaderManager().getLoader(mLoaderId) == null) {
getActivity().getSupportLoaderManager().initLoader(mLoaderId, null, this).forceLoad();
} else {
getActivity().getSupportLoaderManager().restartLoader(mLoaderId, null, this).forceLoad();
}
}
}
AdapterCode:
public class ListAdapter extends CursorAdapter{
public ListAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = LayoutInflater.from(context).inflate(R.layout.item_layout,parent,false);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView name,price;
name = (TextView)view.findViewById(R.id.itemName);
price = (TextView)view.findViewById(R.id.itemPrice);
name.setText(cursor.getString(cursor.getColumnIndex(TableName.table1.mName)));
price.setText(String.valueOf(cursor.getInt(cursor.getColumnIndex(TableName.table1.mPrice))));
}
}
因爲你的ContentProvider爲_id列返回0 ......或者你在你的適配器實現中重寫getItemId ......但是這只是一個盲目的猜測,因爲你沒有提供最重要的代碼...... – Selvin