我想在单击列表视图中的项目时查看文本并将其设置为文本视图,它应该启动新活动,我想将textviews中的文本从数据库设置为该活动......
我该怎么办? 我试着这样做,但它根本不起作用..
我会在onItemClickListener()中使用游标和意图吗?
有人可以告诉我,我该怎么办呢?
请帮忙!谢谢..
package com.example.test;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class DisplayActivity extends Activity {
private DbHelper mHelper;
private SQLiteDatabase dataBase;
private ArrayList<String> bookId = new ArrayList<String>();
private ArrayList<String> book_Name = new ArrayList<String>();
private ArrayList<String> book_Author = new ArrayList<String>();
private ArrayList<String> book_Info = new ArrayList<String>();
private ArrayList<String> book_Isbn = new ArrayList<String>();
private ArrayList<String> book_Copy = new ArrayList<String>();
public static String bNAME="bname";
public static String bAUTHOR="bauthor";
public static String bISBN="bisbn";
public static String bCOPY="bcopy";
public static String bINFO="binfo";
public static String bID="ID";
private ListView userList;
private TextView t1,t2,t3,t4,t5;
private AlertDialog.Builder build;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_activity);
t1=(TextView) findViewById(R.id.name);
t2=(TextView) findViewById(R.id.author);
t3=(TextView) findViewById(R.id.isbn);
t4=(TextView) findViewById(R.id.copy);
t5=(TextView) findViewById(R.id.info);
userList = (ListView) findViewById(R.id.List);
mHelper = new DbHelper(this);
userList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
//what I should write here ?????
}
@Override
protected void onResume() {
displayData();
super.onResume();
}
/**
* displays data from SQLite
*/
private void displayData() {
dataBase = mHelper.getWritableDatabase();
Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
+ DbHelper.TABLE_NAME, null);
bookId.clear();
book_Name.clear();
book_Author.clear();
book_Isbn.clear();
book_Copy.clear();
book_Info.clear();
if (mCursor.moveToFirst()) {
do {
bookId.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ID)));
book_Name.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_NAME)));
book_Author.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_AUTHOR)));
book_Isbn.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ISBN)));
book_Copy.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_COPIES)));
book_Info.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_INFO)));
} while (mCursor.moveToNext());
}
DisplayAdapter disadpt = new DisplayAdapter(DisplayActivity.this,bookId, book_Name);
userList.setAdapter(disadpt);
mCursor.close();
}
}
DisplayAdapter.java
package com.example.test;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class DisplayAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> id;
private ArrayList<String> bookName;
//private ArrayList<String> lastName;
public DisplayAdapter(Context c,ArrayList<String> id,ArrayList<String> bname) {
this.mContext = c;
this.id = id;
this.bookName = bname;
//this.lastName = lname;
}
public int getCount() {
// TODO Auto-generated method stub
return id.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int pos, View child, ViewGroup parent) {
Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.listcell, null);
mHolder = new Holder();
mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
mHolder.txt_bName = (TextView) child.findViewById(R.id.txt_bname);
//mHolder.txt_lName = (TextView) child.findViewById(R.id.txt_lName);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.txt_id.setText(id.get(pos));
mHolder.txt_bName.setText(bookName.get(pos));
//mHolder.txt_lName.setText(lastName.get(pos));
return child;
}
public class Holder {
TextView txt_id;
TextView txt_bName;
}
}
答案 0 :(得分:0)
I will uses cursor and intent in my onItemClickListener() right ?
您不必再次使用光标,因为您已经使用ArrayList填充了适配器,并且您的Arraylist包含所有必需的数据,例如book_id,book_Name
尝试以下代码
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
String bookName = book_Name.get(arg2); //arg2 is the position where clicked
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("Book_Name", bookName);
startActivity(intent);
}
在你的第二个活动中
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
TextView tv1 = (TextView)findViewById(R.id.textView1);
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
tv1.setText(bundle.getString("Book_Name"));
}
}