2017-02-24 79 views
-3

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)))); 
} 
} 
+0

因爲你的ContentProvider爲_id列返回0 ......或者你在你的適配器實現中重寫getItemId ......但是這只是一個盲目的猜測,因爲你沒有提供最重要的代碼...... – Selvin

回答

0

您正在做新的ListAdapter(getActivity(),null);這是一個自定義類,因爲您無法通過界面來做到這一點。你是否更新了默認的getItemId方法?我不會賭錢。

關閉我的頭頂,您的適配器將需要類似的實現:

@Override 
public long getItemId(int position) { 
    long result = 0; 
    if (mCursor.moveToPosition(position)) { 
     Log.d("SO", DatabaseUtils.dumpCurrentRowToString(mCursor); 
     result = mCursor.getLong(mCursor.getColumnIndex(TableName.table1.mId)); 
    } 
    return result; 
} 

如果無法使用解決問題就來看看數據本身在你的光標DatabaseUtils

+0

我已經更新了帖子,代碼爲 – Nsnik

+0

是的,你正在擴展光標適配器,所以這應該工作,只需補充一點方法轉換爲ListAdapter(將bw命名爲具有類似接口的具體類是個壞主意。你的IDE應該警告你) –

+0

不是它沒有解決錯誤仍然返回的值爲零 – Nsnik