2

I am filling a listview from names of a database. That is all working fine. After clicking on an Listitem with the name i would like to set the onListItemClickto fill out textviews in another activity. That is where i am stuck.

Here the code of my listview activity which is working:

public class DataListView extends ListActivity {

private ArrayList<String> results = new ArrayList<String>();

private SQLiteDatabase newDB;


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

    openAndQueryDatabase();

    displayResultList();


}
private void displayResultList() {
    ListView hotelslist = (ListView) findViewById(android.R.id.list);
    hotelslist.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, results));
   getListView().setTextFilterEnabled(true);



}

 @Override
  public void onListItemClick(ListView l, View v, int position, long id) {

     super.onListItemClick(l, v, position, id);
      Toast.makeText(getBaseContext(), l.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();


  }

private void openAndQueryDatabase() {
    try {

        DataBaseHelper newDB = new DataBaseHelper(this, "mydatabase.sqlite");

        SQLiteDatabase sdb = newDB.getReadableDatabase();


        Cursor c = sdb.rawQuery("SELECT name FROM hotel ORDER BY name ASC", null);



if (c != null ) {
    if  (c.moveToFirst()) {
        int i = 0;
        do {
            i++;
            String name = c.getString(c.getColumnIndex("name"));


            results.add(name);
        }while (c.moveToNext());
    } 

}       



    } catch (SQLiteException se ) {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
    } finally {
        if (newDB != null) 

            newDB.close();
    }

}

} 

Besides the name the table also contains columns "address" and "telephone". This data i would like to be filled in textviews "addressfill" and "telephonefill" of another activity with following xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
 <TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     />
 <TextView
    android:id="@+id/address"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Address" />
 <TextView
    android:id="@+id/addressfill"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
  <TextView
    android:id="@+id/telephone"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Telephone number" />
  <TextView
    android:id="@+id/telephonefill"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />


</LinearLayout>

I tried to read into many tutorials out there and many posts here but didnt get around to found the right answer.

What code would i have to use for the onListItemClick and in my activity that uses the xml I presented. Help is very appreciated!

4

2 回答 2

1

如果要在列表项上设置侦听器,则需要扩展数组适配器并将侦听器代码放入适配器中:

public class SettingsListAdapter extends ArrayAdapter<String> {

     @Override
     public View getView(int position, View convertView, ViewGroup parent){
          // put your listener in here
                  mybutton.setOnClickListener(new View.OnClickListener() {          
                @Override
                public void onClick(View v) {
                    // do work here
                }

     }


}

换句话说,simplelistadapter 不足以满足您的需求,您需要自己创建。

编辑

这是创建您自己的自定义 ArrayAdapter 的一个很好的示例:自定义数组适配器教程

于 2012-10-07T15:49:46.897 回答
0

onListItemClick()创建一个 Intent 并将名称放入 Intent 中:

Intent intent = new Intent(DataListView.this, SecondActivity.class);
intent.putExtra("name", l.getItemAtPosition(position).toString());
startActivity(intent);

您必须更改SecondActivity为将接收该名称的 Activity 的类名称。

在第二个 Activity 中onCreate(),读取字符串:

String name = getIntent().getStringExtra("name");
if(name != null) {
    // Do something with the name, like selecting the address 
    //   and telephone number from your database to display
}

但是,由于您正在使用数据库,因此您确实应该使用SimpleCursorAdapter。使用光标更加有用和高效。


我该如何处理表格的其他列数据,如地址和电话?

你有两个选择:

  1. 在您的第二个活动中,使用如下查询:

    "SELECT address, telephone FROM Hotel WHERE name = '" + name + "'"
    

    但是如果多个酒店同名,你会得到不止一个地址...

  2. 将您的第一个活动中的查询更改为:

    "SELECT name, address, telephone FROM Hotel ORDER BY name"
    

    现在onListItemClick()您可以同时传递姓名、地址和电话号码,但是您需要将地址和电话号码存储在某个地方……这就是SimpleCursorAdapter 非常有用的地方。

于 2012-10-07T17:27:28.463 回答