当用户单击单个项目时,如何在textview中显示listview数据

时间:2017-03-25 08:21:00

标签: java android listview

我是android的新手,请有人帮助我。我想在textview中显示listview的单项数据,这是我的下一个活动。数据存储在sqlite中。请告诉我step.thankyou提前

数据库助手类

public class DatabaseHandler extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "DATA.db";
public static final String TABLE_NAME = "TITLE";
public static final String COL0 = "ID";
public static final String COL1 = "TODO";
public static final String COL2 = "DETAIL";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER         PRIMARY KEY AUTOINCREMENT, TODO TEXT, DETAIL TEXT)";
    db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop if database is exists" + TABLE_NAME);
    onCreate(db);

}

public boolean addData(String todo, String detail) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL1, todo);
    contentValues.put(COL2, detail);

    Long result = db.insert(TABLE_NAME, null, contentValues);
    if (result == -1) {
        return false;
    } else {

        return true;
    }
}

public Cursor getListContents() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor data = db.rawQuery(" SELECT * FROM " + TABLE_NAME, null);
    return data;
}

` 主要活动

public class MainActivity extends AppCompatActivity {
DatabaseHandler db;
Button bt_createnew;
ListView lst_1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    db = new DatabaseHandler(this);
    bt_createnew = (Button) findViewById(R.id.btn_new);
    bt_createnew.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, Main2Activity.class);
            startActivity(i);

        }
    });
    lst_1 = (ListView) findViewById(R.id.lst_one);
    lst_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, "item " + position, Toast.LENGTH_LONG).show();
            Cursor data=db.getListContents();
            if (data.getCount()==0){
                display("ERROR","DATA NOT FOUND");
                return ;
            }

            StringBuffer buffer=new StringBuffer();
            while (data.moveToNext()){
                buffer.append("TODO "+ data.getString(1)+"\n");
                buffer.append("DETAIL: "+ data.getString(2)+"\n");
                display("All Store Data:",buffer.toString());
            }
        }
    });
    ArrayList<String> thelist = new ArrayList<>();
    Cursor data = db.getListContents();
    if (data.getCount() == 0) {
        Toast.makeText(MainActivity.this, "database is empty", Toast.LENGTH_LONG).show();
    } else while (data.moveToNext()) {
        thelist.add(data.getString(1));
        ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, thelist);
        lst_1.setAdapter(listAdapter);
    }
}
public void display(String tiltle1,String message) {
    Intent a = new Intent(MainActivity.this, detail.class);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(tiltle1);
    builder.setMessage(message);
    builder.show();


    startActivity(a);
}

Main2Activity类

public class Main2Activity extends AppCompatActivity {

DatabaseHandler db;
Button bt_done;
EditText et_title,et_detail;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    db = new DatabaseHandler(this);
    et_title=(EditText)findViewById(R.id.title);


    et_detail=(EditText)findViewById(R.id.detail);

    bt_done=(Button)findViewById(R.id.btn_done);
    bt_done.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent a = new Intent(Main2Activity.this, MainActivity.class);
            startActivity(a);
            String newEntry= et_title.getText().toString();
            String entrydetail= et_detail.getText().toString();

            if (et_title.length() != 0) {
                addData(newEntry,entrydetail);
                et_title.setText("");

            }
            else{
                Toast.makeText(Main2Activity.this,"you must fill the title",Toast.LENGTH_LONG).show();
            }


            if (et_detail.length() != 0) {
                addData(newEntry,entrydetail);
                et_detail.setText("");

            }
            else{
                Toast.makeText(Main2Activity.this,"you must fill the detail",Toast.LENGTH_LONG).show();
            }


        }
    });

添加数据方法

    public void addData( String newEntry,String entrydetail){


    boolean insertdata = db.addData(newEntry,entrydetail);

    if (insertdata == true){
        Toast.makeText(Main2Activity.this, "data is added", Toast.LENGTH_LONG).show();
    }
    else {
        Toast.makeText(Main2Activity.this, "data is not added", Toast.LENGTH_LONG).show();
    }


}

1 个答案:

答案 0 :(得分:0)

使用position获取SQLite

中的数据

示例

  lst_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> listView, View view, final int position, long id) {
                List o = (List) adapter.getItem(position);
                String month = o.getMonthAndYear();
                int id1 = Integer.parseInt(o.getID()+"");
     }