Android 意图数据显示不正确

Android 意图数据显示不正确,android,Android,我遇到了一个问题,当我进入下一个活动并传递数据时,它只是显示数据库中的最后一个条目,而不是单击的项目信息 .爪哇 公共类TimsFavs扩展了ListActivity实现的ListListener{ /**在首次创建活动时调用*/ ArrayList结果=新建ArrayList(); //数据库结果 字符串col0,col1,col2,col3,col4,col5,col6,col7; @凌驾 创建时的公共void(Bundle savedInstanceState) { super.onCre

我遇到了一个问题,当我进入下一个活动并传递数据时,它只是显示数据库中的最后一个条目,而不是单击的项目信息

.爪哇

公共类TimsFavs扩展了ListActivity实现的ListListener{
/**在首次创建活动时调用*/
ArrayList结果=新建ArrayList();
//数据库结果
字符串col0,col1,col2,col3,col4,col5,col6,col7;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.timsfavs);
DBAdapter db=新的DBAdapter(此);
//---获取所有位置---
db.open();
游标c=db.getAllLocation();
if(c.moveToFirst())
{
做{
col0=c.getString(c.getColumnIndex(DBAdapter.KEY_ROWID));
col1=c.getString(1);
col2=c.getString(2);
col3=c.getString(3);
col4=c.getString(4);
col5=c.getString(5);
col6=c.getString(6);
col7=c.getString(7);
结果:添加(col0);
}而(c.moveToNext());
}
db.close();
setListAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,results));
ListView lv;
lv=getListView();
lv.setTextFilterEnabled(真);
lv.setBackgroundColor(Color.rgb(83,05,14));
lv.setOnItemClickListener((OnItemClickListener)this);
}
public void onItemClick(适配器视图TimsFavs、视图视图、内部位置、长id){
Intent i=新Intent(getApplicationContext(),TimsFavsMore.class);
i、 putExtra(“ct_id_pass”,col0);
i、 putExtra(“ct\u storeid\u pass”,第1栏);
i、 putExtra(“ct地址通行证”,col2);
i、 putExtra(“ct_city_pass”,col3);
i、 putExtra(“ct_省_通行证”,col4);
i、 putExtra(“ct_country_pass”,col5);
i、 putExtra(“ct\U pcode\U pass”,col6);
i、 putExtra(“ct_phnum_pass”,col7);
星触觉(i);
完成();
}
}

我的另一项活动是

`公共类TimsFavsMore扩展活动{ DBAdapter db=新的DBAdapter(此)

字符串rowId;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.timsfavsmore);
Intent i=getIntent();
Bundle b=i.getExtras();
最终字符串rowId=b.getString(“ct_id_pass”);
最终字符串phnum=b.getString(“ct_phnum_pass”);
最终字符串storeid=b.getString(“ct_storeid_pass”);
最终字符串地址=b.getString(“ct_地址_通行证”);
最终字符串city=b.getString(“ct_city_pass”);
最终字符串省=b.getString(“ct_省_通行证”);
最终字符串国家=b.getString(“ct_国家/地区通行证”);
最终字符串pcode=b.getString(“ct_pcode_pass”);
TextView title=(TextView)findViewById(R.id.TextView);
title.setText(Html.fromHtml(“
行ID:+rowId+”
位置地址:

商店ID+”

“+Address+”
“+city+”,“+province+”
”,“+country+”
“+pcode+”
联系信息:


“+phnum+”
); //对导航开放 最终按钮button1=(按钮)findViewById(R.id.gohere); button1.setOnClickListener(新视图.OnClickListener(){ 公共void onClick(视图v){ String uri=“google.navigation:q=Tim%20Hortons、%20“+地址+”、%20“+城市+”、%20“+省份+”、%20“+代码+”; Intent i2=新的Intent(Intent.ACTION_视图,Uri.parse(Uri)); 星触觉(i2); } }); //向地图开放 最终按钮按钮2=(按钮)findViewById(R.id.showmap); button2.setOnClickListener(新视图.OnClickListener(){ 公共void onClick(视图v){ String uri2=“geo:0,0?q=Tim%20Hortons、%20“+地址+”、%20“+城市+”、%20“+省+”、%20“+代码+”; Intent i3=新的Intent(Intent.ACTION_视图,Uri.parse(uri2)); 星触觉(i3); } }); //添加到我的Timmies列表中 最终按钮按钮3=(按钮)findViewById(R.id.removefav); button3.setOnClickListener(新视图.OnClickListener(){ 公共void onClick(视图v){ //---添加2个SQLite--- db.open(); if(db.deleteLocation(rowId)) Toast.makeText(getApplicationContext(),“删除成功”, Toast.LENGTH_LONG).show(); 其他的 Toast.makeText(getApplicationContext(),“删除失败”, Toast.LENGTH_LONG).show(); db.close(); Intent i=newintent(getApplicationContext(),MyTimmies.class); 星触觉(i); 完成(); } }); 最终按钮button4=(按钮)findViewById(R.id.favsdone); button4.setOnClickListener(新视图.OnClickListener(){ 公共void onClick(视图v){ Intent i4=新的Intent(getApplicationContext(),MyTimmies.class); 星触觉(i4); 完成(); } }); }
}`


有什么想法吗?

您正在迭代光标并将值分配给字符串。当它完成迭代时,字符串只包含最后分配的值,然后传递该值


您应该将列表绑定到SimpleCorsorAdapter,而不是创建一个完全不同的字符串数组。然后,在onClickListener中,您只需传递标识数据库中数据行的键。

是的,现在我只需要找出它的编码,感谢您的指导。查看记事本示例-它有一个简单的列表视图,可以绑定到光标,应该为您提供一个良好的启动点。我看了一下笔记本,看不懂它的头绪。
public class TimsFavs extends ListActivity implements OnItemClickListener {

/** Called when the activity is first created. */
ArrayList<String> results = new ArrayList<String>();

// Database results
String col0,col1,col2,col3,col4,col5,col6,col7;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timsfavs);
    DBAdapter db = new DBAdapter(this);

    //---get all Locations---
    db.open();

    Cursor c =  db.getAllLocation();

    if (c.moveToFirst())
    {
        do {

            col0 =  c.getString(c.getColumnIndex(DBAdapter.KEY_ROWID));
            col1 =  c.getString(1);
            col2 =  c.getString(2);
            col3 =  c.getString(3);
            col4 =  c.getString(4);
            col5 =  c.getString(5);
            col6 =  c.getString(6);
            col7 =  c.getString(7);

            results.add(col0);

        } while (c.moveToNext());
    }
    db.close();

    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
    ListView lv;
    lv = getListView();
    lv.setTextFilterEnabled(true);
    lv.setBackgroundColor(Color.rgb(83, 05, 14));
    lv.setOnItemClickListener((OnItemClickListener) this);

}

public void onItemClick(AdapterView<?> TimsFavs, View view, int position, long id) {

    Intent i = new Intent(getApplicationContext(), TimsFavsMore.class);

    i.putExtra("ct_id_pass", col0);
    i.putExtra("ct_storeid_pass", col1);
    i.putExtra("ct_address_pass", col2);
    i.putExtra("ct_city_pass", col3);
    i.putExtra("ct_province_pass", col4);
    i.putExtra("ct_country_pass", col5);
    i.putExtra("ct_pcode_pass", col6);
    i.putExtra("ct_phnum_pass", col7);

    startActivity(i);
    finish();

}
String rowId;

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

        Intent i = getIntent();
        Bundle b = i.getExtras();

         final String rowId = b.getString("ct_id_pass");
         final String phnum = b.getString("ct_phnum_pass");
         final String storeid = b.getString("ct_storeid_pass");
         final String address = b.getString("ct_address_pass");
         final String city = b.getString("ct_city_pass");
         final String province = b.getString("ct_province_pass");
         final String country = b.getString("ct_country_pass");
         final String pcode = b.getString("ct_pcode_pass");

        TextView title = (TextView) findViewById(R.id.textview);
        title.setText(Html.fromHtml("<br>Row ID: " + rowId + "<br><b><u>Location Address:</u></b><br><br>Store #" + storeid + "<br><br>" + address + "<br>" + city + ", " + province + "<br>" + country +"<br>" + pcode +"<br><br><b><u>Contact Info:</b></u><br><br>" + phnum +"<br>"));

// open to Nav

final Button button1 = (Button) findViewById(R.id.gohere);
button1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        String uri = "google.navigation:q=Tim%20Hortons,%20" + address + ",%20" + city + ",%20" + province + ",%20" + pcode + "";
        Intent i2 = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
        startActivity(i2);

    }
});

// open to maps

final Button button2 = (Button) findViewById(R.id.showmap);
button2.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        String uri2 = "geo:0,0?q=Tim%20Hortons,%20" + address + ",%20" + city + ",%20" + province + ",%20" + pcode + "";
        Intent i3 = new Intent(Intent.ACTION_VIEW, Uri.parse(uri2));
        startActivity(i3);

    }
});

// Add to My Timmies List

final Button button3 = (Button) findViewById(R.id.removefav);
button3.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

      //---add 2 SQLite---

        db.open();

        if (db.deleteLocation(rowId))
            Toast.makeText(getApplicationContext(), " Delete successful.", 
                Toast.LENGTH_LONG).show();
        else
            Toast.makeText(getApplicationContext(), "Delete failed.", 
                Toast.LENGTH_LONG).show();            
        db.close();

       Intent i = new Intent(getApplicationContext(), MyTimmies.class);
       startActivity(i);
       finish();

    }
});


final Button button4 = (Button) findViewById(R.id.favsdone);
button4.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

   Intent i4 = new Intent(getApplicationContext(), MyTimmies.class);
       startActivity(i4);
       finish();

    }
});
 }