0

我必须创建 sqlite 数据库并且值显示在 logcat 中。我的问题是我想在模拟器中显示 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();
        }

   });
}}
4

1 回答 1

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.
于 2013-04-21T16:33:37.883 回答