0

我一直在寻找很多问题,但他们没有问或回答我在寻找什么,所以这就是我想要做的。到目前为止,我在女巫中有一个项目,我添加了一个预填充的数据库,其中包含一个包含 6 列数据的表,主 xml 只显示列表视图,我正在使用适配器、光标、SQLiteHelper 等。一切正常并且显示我的自定义 itemlist.xml 一切都很好,现在在列表的每个项目中,我只显示每条记录的 3 件事(第二、第四和第五列数据)(它们都包含我之前提到的 6 个列),我希望能够使用 onclickitemlistener 在 toast 中显示该项目第三列中的数据,我该如何实现?我能够显示列表项 id,所以有人可以在这里帮助我,谢谢这里是 som 代码:​​来自 y 主要活动的代码

public class DBlistActivity extends Activity {

private mediosHelper dbmediosHelper = null;
private Cursor ourCursor = null;
FancyAdapter adapter = null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ListView myListView = (ListView) findViewById(R.id.mylistView);

    dbmediosHelper = new mediosHelper(this);
    try {
        dbmediosHelper.createDataBase();
} catch (IOException ioe) {
    throw new Error("Unable to create database");
}
try {
    dbmediosHelper.openDataBase();
}catch(SQLException sqle){
    throw sqle;
}
try {   
ourCursor = dbmediosHelper.getCursor();
startManagingCursor(ourCursor);
}catch(SQLException sqle){

    throw sqle;

}

adapter = new FancyAdapter(ourCursor);
myListView.setAdapter(adapter);

myListView.setOnItemClickListener(onListClick);

}

class FancyAdapter extends CursorAdapter {
        FancyAdapter(Cursor c) {
                super (DBlistActivity.this, c);
        }
                public void bindView(View row, Context ctxt, Cursor c) {
                    ViewHolder holder = (ViewHolder) row.getTag();
                    holder.populateForm(c, dbmediosHelper);
}
                public View newView(Context ctxt, Cursor c, ViewGroup parent){
                    LayoutInflater inflater = getLayoutInflater();
                    View row = inflater.inflate(R.layout.listitem, parent, false);
                    ViewHolder holder = new ViewHolder(row);
                    row.setTag(holder);
                    return(row);
                }
}

class ViewHolder{
    public TextView nombre=null;
    public TextView estado=null;
    public TextView ciudad=null;
    ViewHolder (View row){
        nombre = (TextView) row.findViewById(R.id.medioName);
        estado = (TextView) row.findViewById(R.id.medioEstado);
        ciudad = (TextView) row.findViewById(R.id.medioCiudad);
    }
    void populateForm(Cursor c, mediosHelper r){
        nombre.setText(r.getName(c));
        estado.setText(r.getEstado(c));
        ciudad.setText(r.getCiudad(c));
    }
}

private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
        Toast.makeText(getApplicationContext(), String.valueOf(id), Toast.LENGTH_SHORT) .show();    
    }
};

从我的 mediosHelper

public Cursor getCursor() {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(TABLE_NAME);
    String[] asColumsToReturn = new String[] { COLUMN_ID, COLUMN_CODE, COLUMN_NAME, COLUMN_URL, COLUMN_ESTADO, COLUMN_CIUDAD};
    Cursor mCursor = queryBuilder.query(myDataBase, asColumsToReturn, null, null, null, null, null);
    return mCursor;
}
public CharSequence getId(Cursor c) {
    return (c.getString(0));
}
public CharSequence getCode(Cursor c) {
    return (c.getString(1));
}
public String getName(Cursor c){
    return(c.getString(2));
}
public String getUrl(Cursor c) {
    return (c.getString(3));
}
public String getEstado(Cursor c) {
    return (c.getString(4));
}
public String getCiudad(Cursor c) {
    return (c.getString(5));
}
4

1 回答 1

1

您需要获取用于填充适配器的光标。一旦你用 c.moveToPosition(position) 将它移动到你在 onItemClickedCallback 中的位置。比您可以访问所有列 (cursor.getColumnIndex("_id"))

编辑:

class MyActivity extends Activity implements OnItemClickListener{
FancyAdapter adapter = null;
...

adapter = new FancyAdapter(ourCursor);
myListView.setAdapter(adapter);

myListView.setOnItemClickListener(this);

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    // TODO Auto-generated method stub
Cursor c = adapter.getCursor();
    c.moveToPosition(position);
    int id = c.getInt(c.getColumnIndex("_id");
}
}
于 2012-04-09T21:25:09.490 回答