2014-01-16 47 views
0

好吧,我搜索扔在这里,根本没有找到我的答案。我的Android应用程序假设要做的是从我的php服务器上获取一个列表,然后将它存储在手机上的数据库中。这一切都有效,我甚至可以用我的sqlite数据库中的信息创建一个列表视图。当我被困在一个新的编码器试图找出如何显示信息,当他们点击列表视图项目,将带他们到另一个页面,将显示该列表视图的所有细节。sqlite数据库和列表视图

所以总之,我想要的是我的列表视图,例如项目1,项目2,项目3,然后当他们点击项目2时,它获取项目2的信息并显示它。有人能指出我正确的方向,我知道我必须使用setOnClickListener,但只是不确定如何使它看起来特定的项目。

public class Events extends Activity 
{ 
// Identifies a particular Loader being used in this component 
String event_name, event_time, event_price, event_loc; 
static JSONObject object =null; 

DBAdapter myDb; 

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

    openDB(); 
    new PrefetchData().execute(); 

    // used to refresh my page. 
    Button button = (Button) findViewById(R.id.refresh); 

    button.setOnClickListener(new OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      openDB(); 
      new PrefetchData().execute(); 
     } 
    }); 
} 

@Override 
protected void onDestroy() 
{ 
    super.onDestroy(); 
    closeDB(); 
} 

private void openDB() 
{ 
    myDb = new DBAdapter(this); 
    myDb.open(); 
} 
private void closeDB() 
{ 
    myDb.close(); 
} 

private class PrefetchData extends AsyncTask<Void, Void, Void> 
{ 

    @Override 
    protected void onPreExecute() 
    { 
     super.onPreExecute();  
    } 

    @Override 
    protected Void doInBackground(Void... arg0) 
    { 
     myDb.deleteAll(); 
     JsonParser jsonParser = new JsonParser(); 
     String json = jsonParser.getJSONFromUrl("http://www.website.com/test.json"); 

     Log.e("JSON Response: ", "> " + json); 

     if (json != null) 
     { 
      try 
      { 
       JSONObject parent = new JSONObject(json); 
       JSONArray eventDetails = parent.getJSONArray("event"); 

       for(int i=0; i < eventDetails.length(); i++) 
       { 
        object = eventDetails.getJSONObject(i); 
        event_name = object.getString("event_name"); 
        event_time = object.getString("event_time"); 
        event_price = object.getString("event_price"); 
        event_loc = object.getString("event_loc"); 
        //event_pic = object.getString("event_pic"); 

        myDb.insertRow(event_name,event_time,event_price,event_loc); 

        Log.e("JSON", "> " + event_name + event_time + event_price + event_loc); 
       } 
      } catch (JSONException e) 
       { 
       // TODO Auto-generated catch block 
       Log.e("Json Error", "Error: " + e.toString()); 
        e.printStackTrace(); 
       } 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) 
    {   
     super.onPostExecute(result); 
     populateListViewFromDB(); 
     myDb.close(); 
     // After completing http call 
     // will close this activity and lauch main activity 
     //Intent i = new Intent(MainActivity.this, Events.class); 
     //i.putExtra("event_name", event_name); 
     //i.putExtra("event_time", event_time); 
     //i.putExtra("event_price", event_price); 
     //startActivity(i); 

     // close this activity 
     //finish(); 
    } 

} 

@SuppressWarnings("deprecation") 
private void populateListViewFromDB() 
{ 
    Cursor cursor = myDb.getAllRows(); 

    // Allow activity to manage lifetime of the cursor. 
    // DEPRECATED! Runs on the UI thread, OK for small/short queries. 
    startManagingCursor(cursor); 

    // Setup mapping from cursor to view fields: 
    String[] fromFieldNames = new String[] 
      {DBAdapter.KEY_NAME, DBAdapter.KEY_TIME, DBAdapter.KEY_PRICE, DBAdapter.KEY_LOC}; 
    int[] toViewIDs = new int[] 
      {R.id.item_name};//,  R.id.item_time};//,   R.id.item_price,  R.id.item_loc}; 

    // Create adapter to may columns of the DB onto elemesnt in the UI. 
    SimpleCursorAdapter myCursorAdapter = 
      new SimpleCursorAdapter(
        this,  // Context 
        R.layout.item_layout, // Row layout template 
        cursor,     // cursor (set of DB records to map) 
        fromFieldNames,   // DB Column names 
        toViewIDs    // View IDs to put information in 
        ); 

    // Set the adapter for the list view 
    ListView myList = (ListView) findViewById(R.id.listViewfordb); 
    myList.setAdapter(myCursorAdapter); 
} 

} 

回答

0

D不要使用setOnClickListener。 一定要使用CursorAdapter并在你的数据库中指定一个ID列。 当您自动设置protected void onListItemClick(ListView l, View v, int position, long id)时,id是数据库中的ID。这样你可以通过一个intent来传递它,并使用id从db中获取数据。

希望它很清楚。对不起,我在匆忙

相当写在代码中只需添加

myList.setOnItemClickListener(
     new OnItemClickListener() 
     { 
      @Override 
      public void onItemClick(AdapterView<?> arg0, View view, 
        int position, long id) { 

         //here the id value is what you need to take data from the db filtering by id 
       } 
      } 
    ); 
+0

我想我有点明白你的意思..我用光标..这是我为它的代码。请查看我编辑的代码,用于从我的数据库中获取信息。 – Jayce

+0

好的,找出那个地方。我如何获得信息传递给下一个活动? Intent intent = new Intent(getBaseContext(),ListInfo.class); \t intent.putExtra(“ID”,position); \t startActivity(intent); – Jayce

+0

你应该通过身份证,而不是位置到下一个活动。 一旦你完成了,你可以使用ID来查询你的数据库并获取数据 –

0

你应该设置监听到你的ListView像这样

private OnItemClickListener onListItemClickListener = new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> adapter, View view, int position, long id) { 
     // TODO YOUR CODE 
    } 
}; 

private OnItemLongClickListener onItemLongClickListener = new OnItemLongClickListener() { 

    @Override 
    public boolean onItemLongClick(AdapterView<?> adapter, View view, int position, long id) { 
     YOUR_CODE 
    } 
}; 

这是两个类型,longClicj,只需点击,你可以用它两个或一个,它取决于你的需要

也onCreate方法,你应该设置收听者列表查看

listView.setOnItemClickListener(onListItemClickListener); 
listView.setOnItemLongClickListener(onItemLongClickListener); 
相关问题