我需要将数据从数据库传递到下一个活动,该数据与所选项目位于同一行。我试过这样做,但是它显示了列表视图中的所有数据。我需要在列表视图项目中显示标题并在下一个活动中显示描述。这是我的代码:
package com.example.assfar.travel_guide;
import java.util.ArrayList;
import java.util.List;
import com.example.assfar.R;
import com.example.assfar.database.DBHelper;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class TravelGuide extends ListActivity {
private DBHelper dataBase;
private List<String> guide_title_list= new ArrayList<String>();
private Cursor cursor;
String title;
Integer id;
String desc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.travelguide);
dataBase= DBHelper.instance();
SQLiteDatabase db= dataBase.getWritableDatabase();
cursor= db.rawQuery("select * from Tour_Guide", null);
if(cursor!=null) {
if(cursor.moveToFirst());
{
do
{
desc = cursor.getString(cursor.getColumnIndex("tour_Description"));
title= cursor.getString(cursor.getColumnIndex("guide_title"));
id= cursor.getInt(cursor.getColumnIndex("_id"));
guide_title_list.add("" + title +id +desc );
} while(cursor.moveToNext());
}
}
this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, guide_title_list));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//String selectedItem= ((TextView) v).getText().toString();
String selectedValue= (String) getListAdapter(). getItem(position);
Intent i = new Intent(TravelGuide.this, Travel_Guide_Details.class);
i.putExtra("selected item", selectedValue);
startActivity(i);
}
}
和下一个活动代码:
package com.example.assfar.travel_guide;
import com.example.assfar.R;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.TextView;
public class Travel_Guide_Details extends Activity {
public static Cursor cursor;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.travel_guide_details);
//tour guide description in text view
textView = (TextView)findViewById(R.id.guide_desc);
Intent i= getIntent();
// getting attached intent data
String item = i.getStringExtra("selected item");
// displaying selected product name
textView.setText(item);
} }