从数据库中单击并列出

时间:2018-01-02 16:07:02

标签: java android listview

在这个应用程序中,有一个主要活动,包括按钮,显示食品应用程序中涉及的类别。我们刚刚参与过" Burgers"类别呢。单击Burger Category后,将打开另一个名为" Burgers.java"将添加新按钮和列表视图,显示数据库中的数据。 当我们点击add new,一个活动命名" AddBurgers.java"将打开我们可以在数据库中输入新汉堡的价格和名称的地方,它也将在列表视图中显示。

我已经制作了" ListAdapter.java"和#34; SQLiteHelper.java"有助于将数据添加到数据库然后在列表视图中显示的活动。

现在我们面临的问题是,我们要删除项目长按的列表视图项目,并希望在项目点击时显示项目通知面板中的项目数据。此外,如果我们点击通知,它将打开lisview   的 Burgers.java

    public class Burgers extends Activity {

SQLiteHelper sqLiteHelper;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
ListAdapter listAdapter ;
ListView LISTVIEW;

ArrayList<String> ID_Array;
ArrayList<String> NAME_Array;
ArrayList<String> PRICE_Array;


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

    Button btn = (Button)findViewById(R.id.button5);
    btn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            startActivity(new Intent(Burgers.this, AddBurgers.class));
        }
    });

    LISTVIEW = (ListView) findViewById(R.id.listview1);

    ID_Array = new ArrayList<String>();

    NAME_Array = new ArrayList<String>();

    PRICE_Array = new ArrayList<String>();

    sqLiteHelper = new SQLiteHelper(this);

    LISTVIEW.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            listAdapter.notifyDataSetChanged();

        }
    });

}

@Override
protected void onResume() {

    ShowSQLiteDBdata() ;

    super.onResume();
}

private void ShowSQLiteDBdata() {

    sqLiteDatabase = sqLiteHelper.getWritableDatabase();

    cursor = sqLiteDatabase.rawQuery("SELECT * FROM "+SQLiteHelper.TABLE_NAME+"", null);

    ID_Array.clear();
    NAME_Array.clear();
    PRICE_Array.clear();

    if (cursor.moveToFirst()) {
        do {

            ID_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.COL_1)));

            NAME_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.COL_2)));

            PRICE_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.COL_3)));


        } while (cursor.moveToNext());
    }

    listAdapter = new ListAdapter(Burgers.this,

            ID_Array,
            NAME_Array,
            PRICE_Array
    );

    LISTVIEW.setAdapter(listAdapter);

    cursor.close();
}

}

ListAdapter.java

    public class ListAdapter extends BaseAdapter {

Context context;
ArrayList<String> ID;
ArrayList<String> Name;
ArrayList<String> Price;


public ListAdapter(
        Context context2,
        ArrayList<String> id,
        ArrayList<String> name,
        ArrayList<String> price
)
{

    this.context = context2;
    this.ID = id;
    this.Name = name;
    this.Price = price;
}

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 position, View child, ViewGroup parent) {

    Holder holder;

    LayoutInflater layoutInflater;

    if (child == null) {
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        child = layoutInflater.inflate(R.layout.activity_row_design, null);

        holder = new Holder();

        holder.ID_TextView = (TextView) child.findViewById(R.id.textView2);
        holder.Name_TextView = (TextView) child.findViewById(R.id.textView10);
        holder.PriceTextView = (TextView) child.findViewById(R.id.textView11);

        child.setTag(holder);

    } else {

        holder = (Holder) child.getTag();
    }
    holder.ID_TextView.setText(ID.get(position));
    holder.Name_TextView.setText(Name.get(position));
    holder.PriceTextView.setText(Price.get(position));

    return child;
}



public class Holder {

    TextView ID_TextView;
    TextView Name_TextView;
    TextView PriceTextView;
}
}

SQLiteHelper.java

    public class SQLiteHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "Afoods.db";
public static final String TABLE_NAME = "burger_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "PRICE";

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

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, PRICE TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    onCreate(db);
}

public boolean insertData(String name,String price) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2,name);
    contentValues.put(COL_3,price);
    long result = db.insert(TABLE_NAME,null ,contentValues);
    if(result == -1)
        return false;
    else
        return true;
}

}

AddBurgers.java

public class AddBurgers extends AppCompatActivity {

SQLiteHelper myDb;
EditText editName,editPrice;
Button btnAddData;
Button btnviewAll;

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


    myDb = new SQLiteHelper(this);

    editName = (EditText)findViewById(R.id.editText);
    editPrice = (EditText)findViewById(R.id.editText2);
    btnAddData = (Button)findViewById(R.id.button);
    btnviewAll = (Button)findViewById(R.id.button2);
    AddData();

    btnviewAll.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(AddBurgers.this, Burgers.class);
            startActivity(intent);
        }
    });


}

public  void AddData() {
    btnAddData.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isInserted = myDb.insertData(editName.getText().toString(),
                            editPrice.getText().toString() );
                    if(isInserted == true)
                        Toast.makeText(AddBurgers.this,"Data Inserted",Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(AddBurgers.this,"Data not Inserted",Toast.LENGTH_LONG).show();
                }
            }
    );
}
}

任何帮助都将受到高度赞赏。我已经工作了15天。我上面提到的代码运行得很好。我只需要在其中添加删除和通知功能。

1 个答案:

答案 0 :(得分:0)

  

我们要删除项目长按

上的列表视图项目

为listView实施setOnItemLongClickListener,就像您对onItemClickListener所做的那样。长按一次,对您的数据库执行delete查询,类似于您对insert查询所做的查询。

  

想要在项目点击

上显示通知面板中的项目数据

onItemClickListener中添加通知代码。要显示通知,您必须学习here如何创建通知。

  

此外,如果我们点击通知,它将打开lisview

您必须为PendingIntent创建一个Intent并启动一个将显示listView的活动(Burgers.java)。它清楚地解释了here