2013-04-21 71 views
0

我必須創建sqlite數據庫,並在logcat中顯示值。我的問題是我想在模擬器中顯示ListView。我在這裏附上我的代碼,參考它。如何使用sqlite在模擬器中顯示listview?

公共類AndroidSQLiteTutorialActivity擴展ListActivity {

private static final String KEY_ID = "company_id"; 
private static final String KEY_NAME = "company_name"; 
private static final String KEY_DESC = "company_desc"; 

public static ArrayList<String> ArrayofName = new ArrayList<String>(); 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    DatabaseHandler db = new DatabaseHandler(this); 

    /** 
    * CRUD Operations 
    * */ 
    // Inserting Contacts 
    Log.d("Insert: ", "Inserting .."); 
    db.addContact(new Contact("GNTS Technologies pvt ltd", "Software Company")); 
    //db.addContact(new Contact("HCL","Hardware","Inactive")); 
    // db.addContact(new Contact("Srinivas", "9199999999")); 
    //db.addContact(new Contact("Tommy", "9522222222")); 
    //db.addContact(new Contact("Karthik", "9533333333")); 

    // Reading all contacts 
    Log.d("Reading: ", "Reading all contacts.."); 
    List<Contact> contacts = db.getAllContacts();  

    for (Contact cn : contacts) { 
     String log = "company_id: "+cn.getID()+" ,company_name: " + cn.getName() + " ,company_desc: " + cn.getDesc(); 
      // Writing Contacts to log 
    Log.d("Name: ", log); 
    db.getAllContacts(); 

    } 



    ListView lv = (ListView) findViewById(R.id.listView); 


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.activity_list_item, ArrayofName); 

    setListAdapter(adapter); 

    lv.setOnItemClickListener(new OnItemClickListener() { 


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

      String company_desc = ((TextView) view 
        .findViewById(R.id.company_desc)).getText().toString(); 
      String company_id = ((TextView) view 
        .findViewById(R.id.company_id)).getText().toString(); 
      String company_name = ((TextView) view 
        .findViewById(R.id.company_name)).getText().toString(); 

      Toast.makeText(getApplicationContext(), 
      ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); 
     } 

    }); 
}} 
+0

我不明白,這個問題的標題和問題的文字似乎被問兩個不同的東西。你問如何與Web服務交互,或者你在問如何使用ListView。請編輯您的標題和問題,以澄清您要求的內容。 – 2013-04-21 19:55:56

回答

0
Inorder to list values in listview , 

    Step 1: Use Cursor 

     eg: Cursor c = db.getContact(): // sample 

    Step 2: Create a custom class extending cursorAdapter 

     eg: CustomAdapter adapter = new CustomAdapter(this,cusrsor); // sample 

    Step 3: listview.setadapter(adapter); 


Note:You should implement cursor adapter. 
相關問題