我正在从数据库返回此数据
@Override
public ArrayList < String > returnProducerData(String entereduser_id) {
Cursor c = db.rawQuery("Select * from producer_table WHERE user_id= "+entereduser_id, null);
ArrayList<String> producerResult = new ArrayList<String>();
int ProducerId = c.getColumnIndex(PRODUCER_ID);
int UserId= c.getColumnIndex(USER_ID);
int ProducerName = c.getColumnIndex(PRODUCER_NAME);
int ProducerSurname = c.getColumnIndex(PRODUCER_SURNAME);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
producerResult.add(c.getString(ProducerId) +" "+c.getString(UserId) +" "+ c.getString(ProducerName) +" "+ c.getString(ProducerSurname) );
}
return producerResult;
}
我使用这种方法在列表视图中显示数据
HashMap<String, String> user = session.getUserDetails();
String user_id= user.get(SessionManager.USER_ID);
ArrayList<String> data = info.returnProducerData(user_id);
//set all data as listview using Array
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data));
//set listview action
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String UserInfo = listView.getItemAtPosition(position).toString();
Intent i =new Intent(ShowProducerTable.this, TestDataBase.class);
i.putExtra("UserInfo", UserInfo);
startActivity(i);
/***just to check if it has value***/
// Toast.makeText(getBaseContext(),"position is : "+position+" and value is "+UserInfo,Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), id + "", Toast.LENGTH_LONG).show();
}
});
现在我想查看我单击的行的producter_id。
你能帮帮我吗?
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String UserInfo = listView.getItemAtPosition(position).toString();
String userId = UserInfo.substring(0, UserInfo .indexOf(" "));
Intent i =new Intent(ShowProducerTable.this, TestDataBase.class);
i.putExtra("UserInfo", UserInfo);
startActivity(i);
/***just to check if it has value***/
// Toast.makeText(getBaseContext(),"position is : "+position+" and value is +UserInfo,Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), id + "", Toast.LENGTH_LONG).show();
}
});
请参阅代码中的行"String userId=UserInfo.substring(0,UserInfo.indexOf(");"。。。您将获得用户id。
String productIDAsString;
for(int i= 0; i < UserInfo.length;i+++) {
if(!(Userinfo.substring(i,i+1).equals(" ")) {
productIDAsString += UserInfo.substring(i,i+1);
}
}
int productID = Integer.parseInt(productIDAsString);
使用框架提供的CursorAdapter
实现之一。通过这种方式,getItemAtPosition
返回的位置上的项目,在Cursor
移动的位置上,您可以从中轻松提取所需的信息,避免难以跟踪/调试的黑客攻击,并可能导致严重的崩溃
或者您可以创建另一个POJO类来保存从数据库返回的数据,并使用它的getter方法,您将能够获得所需的结果,如:
public ArrayList < Producer > returnProducerData(String entereduser_id) {
Cursor c = db.rawQuery("Select * from producer_table WHERE user_id= "+entereduser_id, null);
ArrayList<Producer> producerResult = new ArrayList<Producer>();
int ProducerId = c.getColumnIndex(PRODUCER_ID);
int UserId= c.getColumnIndex(USER_ID);
int ProducerName = c.getColumnIndex(PRODUCER_NAME);
int ProducerSurname = c.getColumnIndex(PRODUCER_SURNAME);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
Producer producer = new Producer();
producer.setId(c.getString(ProducerId));
producer.setUserId(c.getString(UserId));
producer.setName(c.getString(ProducerName));
producer.setSurname(c.getString(ProducerSurname));
producerResult.add(producer);
}
return producerResult;
}
然后为您的列表视图使用自定义适配器,在onItemClick方法中,您可以通过调用producer.getUserID()方法来访问id。