嗨,我想用 ListView 构建一个 android 应用程序,如果我在这个 ListView 中选择一个值,我想用这个值启动另一个活动。问题是我不知道如何单击 ListView 中的值而其他活动获取信息。这是我的代码:
我的第一个活动:
package de.android.shilfe;
import de.android.shilfe.DatenbankManager;
import de.android.shilfe.R;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.app.ListActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends ListActivity {
private SQLiteDatabase mDatenbank;
private DatenbankManager mHelper;
private static final String KLASSEN_SELECT_ROW =
"SELECT _id, name FROM Stundenplan";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHelper.STUNDENPLAN_CREATE = "CREATE TABLE Stundenplan(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, "+
"name TEXT NOT NULL)";
mHelper = new DatenbankManager(this);
//what I must do here ??? :(
}
@Override
protected void onPause() {
super.onPause();
mDatenbank.close();
Toast.makeText(this,
getResources().getString(R.string.db_close),
Toast.LENGTH_SHORT).show();
}
@Override
protected void onResume() {
super.onResume();
mDatenbank = mHelper.getReadableDatabase(); //Datenbank öffnen
Toast.makeText(this,
getResources().getString(R.string.db_open),
Toast.LENGTH_SHORT).show();
ladeDaten();
}
private void ladeDaten() {
Cursor KlassenCursor = mDatenbank.rawQuery(KLASSEN_SELECT_ROW, null); // Gibt ein Index zurück
startManagingCursor(KlassenCursor); //Durchläuft diesen
android.widget.SimpleCursorAdapter KlassenAdapter = new android.widget.SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
KlassenCursor,
new String[] {"name"},
new int[] {
android.R.id.text1
});
setListAdapter(KlassenAdapter);
}
public void onButtonClick(View view){
EditText et =(EditText) findViewById(R.id.editText1);
ContentValues werte = new ContentValues();
werte.put("name",et.getText().toString());
mDatenbank.insert("stundenplan", null, werte);
ladeDaten();
et.setText("");
}
}
我的第一个活动布局 xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="5">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/create_defaulttext">
<requestFocus />
</EditText>
<Button
android:id="@+id/sf_daten_datenbanken_einfuegen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sf_daten_datenbanken_einfuegen"
android:onClick="onButtonClick" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView>
<TextView
android:id="@+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tx_daten_datenbank_leer"/>
</LinearLayout>
</LinearLayout>
我使用以下代码在其他应用程序中尝试它:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List valueList = new ArrayList<String>();
for(int i=0;i<10;i++)
{
valueList.add("value" + i);
}
ListAdapter adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,valueList);
final ListView lv = (ListView)findViewById(R.id.listview1);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
Intent intent = new Intent();
intent.setClassName(getPackageName(), getPackageName() + ".Show_Activity");
intent.putExtra("selected",lv.getAdapter().getItem(arg2).toString());
startActivity(intent);
}
});
}
但在另一个应用程序中,我通过 EditText 控件填充 ListView
我需要帮助 :(