【问题标题】:show ListView items from database显示数据库中的 ListView 项目
【发布时间】:2013-10-11 09:25:27
【问题描述】:

我有这个列表视图,其中包含从 SQLite 浏览器检索的项目(类别),现在我希望用户单击其中一个类别,例如,如果用户单击餐厅类别,我想获取 id并将数据库中的所有餐厅检索到新意图活动中的另一个列表中。

这里的问题是,类别中的每个项目都显示相同的结果。

类别活动

public class WhereToGo extends ListActivity {

public final static String ID_EXTRA="com.example.buttontest._id";
  DataBaseHelper db_con;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_where_to_go);

}

 @Override
    public void onStart(){
     super.onStart();
     db_con = new DataBaseHelper(this);
        listNotes();
    }
@SuppressWarnings("deprecation")
private void listNotes(){
     SQLiteDatabase db = db_con.getReadableDatabase();
     try {
         Cursor c = db.rawQuery("SELECT name as '_id' " +
     "FROM category ",null );
         final ListAdapter noteAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2, c,new String[] {"_id"},new int[] {android.R.id.text1});
         this.setListAdapter(noteAdapter);
     } finally {
     db.close();
     }
    }
 @Override
    protected void onListItemClick(ListView l, View v, int position, long id){
     super.onListItemClick(l, v, position, id);
     // Intent:
     Intent i = new Intent(this, ShowWhereToGo.class);
     i.putExtra(ID_EXTRA, String.valueOf(id));
     startActivity(i);

    }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.where_to_go, menu);
    return true;
}

}

ShowWhereToGo

public class ShowWhereToGo extends Activity {

     DataBaseHelper dbhelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_where_to_go);

        String[] from = new String[] { "name" };  
          int[] to = new int[] { R.id.TextView1 };  

          dbhelper = new DataBaseHelper(this);  
          try {  
           dbhelper.createDataBase();  
          } catch (IOException e) {  
           // TODO Auto-generated catch block  
           e.printStackTrace();  
          }  

          Cursor c = dbhelper.getFacilityData();  

          @SuppressWarnings("deprecation")
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.activity_tab, c, from, to);  

           ListView list = (ListView) findViewById(R.id.listView1);  

           list.setAdapter(adapter);  
           list.setOnItemClickListener(new OnItemClickListener() {   
                public void onItemClick(AdapterView<?> parent, View view,int position, long id) {      
                    Intent myIntent = new Intent(view.getContext(), CheckinActivity.class); 
                    myIntent.putExtra("id", position);
                    startActivityForResult(myIntent, 0); // display SubView.class               }
            }

                @SuppressWarnings("unused")
                public void onItemClick1(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub

                }}); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.show_where_to_go, menu);
        return true;
    }

}

【问题讨论】:

    标签: java android sqlite


    【解决方案1】:

    尝试更改此行:

    i.putExtra(ID_EXTRA, String.valueOf(id));
    

    到:

    i.putExtra(ID_EXTRA, position);
    

    【讨论】:

      【解决方案2】:

      1- 在listNotes 方法中获取noteAdapter 并使其成为全局变量。 2- 在onListItemClick 方法中 将此i.putExtra(ID_EXTRA, String.valueOf(id)); 更改为i.putExtra(ID_EXTRA, noteAdapter.getItem(position));

      ** 你必须传递 *_id* 的值,但在这里你传递的是数组中与你正在搜索的 id 完全无关的 String 对象的 is。

      【讨论】:

        猜你喜欢
        • 2018-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多