我一直在搜索很多问题,但他们没有问或回答我正在寻找的东西,所以这就是我想要做的。 到目前为止我有一个关于女巫的项目我添加了一个预先填充的数据库,其中包含一个包含6列数据的表,主xml只显示列表视图,我使用的是Adapter,Cursor,SQLiteHelper等。一切正常,显示我的自定义itemlist.xml都很好,现在在列表的每个项目中我只显示每条记录的3个东西(第二,第四和第五列数据)(它们都包含我之前提到的6个coluns),我希望能够使用onclickitemlistener来显示该项目的第三列数据,我该如何实现?我能够显示列表项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));
}
答案 0 :(得分:1)
你需要获得用于填充适配器的光标。一旦你将它移动到你在onItemClickedCallback中使用c.moveToPosition(position)获得的位置。您可以访问所有列(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");
}
}