当我点击主要活动列表中的某些数据时,我想在我的数据库中将COLUMN_INGRIDIENTS列中的值显示到另一个活动中的TextView。
main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
list.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="vertical" >
<TextView
android:id="@+id/recipeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
Main.java
package com.example.viewer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Main extends Activity {
public final static String TAG_INGRIDIENTS="com.example.getlistfromdb.INGRIDIENTS";
private recipelistHelper dbrecipelistHelper = null;
private Cursor ourCursor = null;
private recipeAdapter adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView myListView = (ListView) findViewById(R.id.myListView);
dbrecipelistHelper = new recipelistHelper(this);
dbrecipelistHelper.createDatabase();
dbrecipelistHelper.openDatabase();
ourCursor=dbrecipelistHelper.getCursor();
startManagingCursor(ourCursor);
adapter = new recipeAdapter(ourCursor);
myListView.setAdapter(adapter);
myListView.setOnItemClickListener(onListClick);
}
catch (Exception e)
{
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
e.printStackTrace();
}
}
private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent i=new Intent(Main.this, Activity2.class);
String ingridients = null;
i.putExtra(TAG_INGRIDIENTS,ingridients);
startActivity(i);
}
};
class recipeAdapter extends CursorAdapter {
recipeAdapter(Cursor c){
super(Main.this, c);
}
@Override
public void bindView (View row, Context ctxt, Cursor c)
{
recipeHolder holder = (recipeHolder)row.getTag();
holder.populateFrom(c, dbrecipelistHelper);
}
@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent)
{
LayoutInflater inflater = getLayoutInflater();
View row=inflater.inflate(R.layout.list, parent, false);
recipeHolder holder = new recipeHolder(row);
row.setTag(holder);
return(row);
}
}
static class recipeHolder {
private TextView name=null;
recipeHolder(View row){
name=(TextView)row.findViewById(R.id.recipeText);
}
void populateFrom(Cursor c, recipelistHelper r){
name.setText(r.getName(c));
}
}
}
Activity2.java
package com.example.viewer;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Activity2 extends Activity {
String ingridients = null;
String procedure = null;
private TextView txtIngridients = null;
private TextView txtProcedure = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
ingridients=this.getIntent().getStringExtra(Main.TAG_INGRIDIENTS);
//procedure=this.getIntent().getStringExtra(Main.TAG_PROCEDURE);
txtIngridients=(TextView)findViewById(R.id.ingridientText);
//txtProcedure=(TextView)findViewById(R.id.procedureText);
txtIngridients.setText(ingridients);
//txtProcedure.setText(procedure);
}
}
view.xml用
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/ingridientText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="29dp"
android:text="A" />
</RelativeLayout>
答案 0 :(得分:1)
使用adapter.getItem
获取ListView中所选行的光标:
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Cursor selectedCursor=(Cursor)adapter.getItem(position);
// get ingridients coulmn value from selectedCursor
selectedCursor.moveToPosition(position);
String ingridients = dbrecipelistHelper.getName(selectedCursor);
// your code here
}
答案 1 :(得分:0)
在onItemClickListener中,为字符串赋值null:
String ingridients = null;
并将此String传递给下一个Activity。这将为null。在初始化ingridients变量并将其设置为所需的String之前,必须获取数据。