如何使用onclick方法从listview获取行的ID?

时间:2014-10-27 08:13:04

标签: android listview get rowid

我从数据库返回此数据

@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;

}   

我正在使用此方法在listview中显示数据

 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();
        }
    });

现在我想看到我点击的行的producer_id。

你能帮我吗?

4 个答案:

答案 0 :(得分:0)

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。

答案 1 :(得分:0)

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);

答案 2 :(得分:0)

使用框架提供的CursorAdapter实现之一。这样,getItemAtPositionCursor位置移动的位置处的项目,您可以从中轻松提取所需信息,避免难以跟踪/调试的黑客攻击,并可能导致讨厌的崩溃

答案 3 :(得分:0)

或者您可以创建另一个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。