单击特定列表视图时如何在新活动中显示字段值?

时间:2014-10-16 16:21:25

标签: android sqlite listview

我的数据库上有一张表。 该表中的一个字段在主活动中显示为列表视图。 我已经实现了setOnItemClickListener来打开新活动,以显示该选定列表项的其他字段值。 新的活动成功开通。 但我在显示字段值时遇到麻烦.... 我浏览了很多网站.... 但没有任何帮助.....

我在listview中显示了部门名称... 在新活动中,我需要获取DEPT_LIST_COLUMN_ID和DEPT_LIST_COLUMN_NAME的值

MainActivity.java

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class MainActivity extends Activity {

private MyDBHelper mydb;
private SimpleCursorAdapter adapter;
public static final String Row_ID = "row_id"; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //********************************//
    //Begin of Database-Table1_Dept_List
    //********************************//
    ListView dplist=(ListView)findViewById(R.id.list_view);
    //Instantiate table1_dept_list
    mydb =new MyDBHelper(this, null, null,4);
    SQLiteDatabase db =mydb.getReadableDatabase();
    //Removing duplicate values from table1_dept_list
    mydb.delete_dep_list();
    //Inserting into table1_dept_list
    mydb.add_dept_list("DLBIOCHEM","BioChemistry");
    mydb.add_dept_list("DLBIOTECH","BioTechnology");
    mydb.add_dept_list("DLBOT", "Botany");
    mydb.add_dept_list("DLCHEM","Chemistry");
    mydb.add_dept_list("DLCOM","Commerce");
    mydb.add_dept_list("DLCS","Computer Science");
    mydb.add_dept_list("DLECO","Economics");
    mydb.add_dept_list("DLEDU","Education");
    mydb.add_dept_list("DLENG","English");
    mydb.add_dept_list("DLEVS","Environmental Science");
    mydb.add_dept_list("DLFSN","Food Science and Nutrition");
    mydb.add_dept_list("DLGEO","Geology");
    mydb.add_dept_list("DLJMC", "Journalism and Massmedia Communication");
    mydb.add_dept_list("DLLIS","Library and Information Science");
    mydb.add_dept_list("DLMATH","Mathematics");
    mydb.add_dept_list("DLMICRO","Microbiology");
    mydb.add_dept_list("DLPE","Physical Education");
    mydb.add_dept_list("DLPHY","Physics");
    mydb.add_dept_list("DLPRIMS","Periyar Institute of Management Studies");
    mydb.add_dept_list("DLPSY","Psychology");
    mydb.add_dept_list("DLSOC","Sociology");
    mydb.add_dept_list("DLTAM", "Tamil");
    mydb.add_dept_list("DLTAD","Textile and Apparel Design");
    mydb.add_dept_list("DLZOO","Zoology");



    //list view 
    Cursor AllDeptList = mydb.get_dept_list();
    String[] from=new String[] { 
            MyDBHelper.DEPT_LIST_COLUMN_NAME
    };
    int[] to=new int[] {R.id.dis_text};
     adapter = new SimpleCursorAdapter(this,R.layout.disp_text,AllDeptList,from,to,0 );

    dplist.setAdapter(adapter);
    //********************************//
    //end of Database-Table1_Dept_List//
    //********************************//
    dplist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            // TODO Auto-generated method stub

            Cursor cursor = mydb.get_dept_list();
            cursor.moveToPosition(position);

            Intent intent_dp_list = new Intent(MainActivity.this,DepartmentDesignation.class);
            intent_dp_list.putExtra(Row_ID,arg3);               
            startActivity(intent_dp_list);
        }
    });
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

DepartmentDesignation.java

package com.example.contact;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.TextView;


public class DepartmentDesignation extends Activity {
private SimpleCursorAdapter adapter;
public static String d_id;
public static String d_name;
private String Row_id;
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.disp_dept_desig);

    TextView tx_id = (TextView)findViewById(R.id.id);
    Intent extras = getIntent();
    tx_id.setText(extras.getStringExtra(Row_id));
    TextView tx_name=(TextView)findViewById(R.id.name);
    tx_name.setText(d_name);


}


}

MyDBHelper.java

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDBHelper extends SQLiteOpenHelper{

public static final String DEPT_LIST_TABLE1_NAME="tbl_dep_list";
public static final String DEPT_LIST_ROW_ID = "_id";
public static final String DEPT_LIST_COLUMN_ID = "fld_tb1_id";
public static final String DEPT_LIST_COLUMN_NAME="fld_tb1_list";



public MyDBHelper(Context context, String name, CursorFactory factory,
        int version) {
    super(context, "contact_book.db", factory, 11);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase database) {
    // TODO Auto-generated method stub
    database.execSQL(" CREATE TABLE " + DEPT_LIST_TABLE1_NAME + "(" + DEPT_LIST_ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + DEPT_LIST_COLUMN_ID + " TEXT, " + DEPT_LIST_COLUMN_NAME + " TEXT ) " );

 }

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS " + DEPT_LIST_TABLE1_NAME);
    onCreate(db);

}



public void add_dept_list(String dl_id, String dep_list) {
    // TODO Auto-generated method stub
    ContentValues values = new ContentValues(2);
    values.put(MyDBHelper.DEPT_LIST_COLUMN_ID , dl_id);
    values.put(MyDBHelper.DEPT_LIST_COLUMN_NAME,dep_list);
    getWritableDatabase().insert(MyDBHelper.DEPT_LIST_TABLE1_NAME,null,values);
}

    public int delete_dep_list(){
        try{
            SQLiteDatabase db =this.getWritableDatabase();
            return db.delete(DEPT_LIST_TABLE1_NAME, null, null);
            }
        catch(Exception e){
            e.printStackTrace();
            }
        return 0;

    }
public Cursor get_dept_list(){
    String[] from = new String[] {MyDBHelper.DEPT_LIST_ROW_ID,MyDBHelper.DEPT_LIST_COLUMN_ID,MyDBHelper.DEPT_LIST_COLUMN_NAME};
    Cursor cursor = getReadableDatabase().query(MyDBHelper.DEPT_LIST_TABLE1_NAME, from,null,null,null, null, null);

    if(cursor != null){
        cursor.moveToFirst();
    }
    return cursor;

}



}

1 个答案:

答案 0 :(得分:0)

onItemClick方法上,传递意图中所需的值以打开活动。 Intent intent = new Intent("blah blah"); intent.putExtra(DEPT_LIST_COLUMN_ID, "value"); intent.putExtra(DEPT_LIST_COLUMN_NAME, "value");

然后在打开的活动上,执行: extras.getStringExtra(DEPT_LIST_COLUMN_ID)