gpt4 book ai didi

java - 无法在我的自定义 ListView 中实现搜索 View

转载 作者:行者123 更新时间:2023-12-01 16:41:27 25 4
gpt4 key购买 nike

我用谷歌搜索并观看了 youtube,但无法实现它。我找到了方法,但我还必须更改我的自定义 ListView 代码。但我想在我的自定义 ListView 中实现它。

还有更难的,但我喜欢这个 ListView 。它很容易实现。

package com.example.inventorymanagment;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class CustomListView extends AppCompatActivity {

static SQLiteDatabase sql;

static ListView lv;
SearchView sv;
static ArrayList midArr = new ArrayList();
static ArrayList mitemNameArr = new ArrayList();
static ArrayList msacPriceStockArr = new ArrayList();
static myAdapter mad;



public void show() {
midArr.clear();
mitemNameArr.clear();
msacPriceStockArr.clear();
mad = new myAdapter(this , midArr ,mitemNameArr ,msacPriceStockArr);

Cursor c = sql.rawQuery("SELECT * FROM item_list", null);

if (c.moveToFirst()) {
do {
midArr.add(c.getInt(0));
mitemNameArr.add(c.getString(1));
msacPriceStockArr.add("Sac/Hsn : "+c.getString(2) + " "
+ "Price : "+c.getInt(3) + " "
+ "Stock : "+c.getInt(4));
//(id ,item_name , sac_hsn , price , stock)
} while (c.moveToNext());
}
lv.setAdapter(mad);
c.close();

}

public void delete(int position){

sql.execSQL("DELETE FROM item_list where ID = " + position + "");
show();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.menu_items , menu);

return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
super.onOptionsItemSelected(item);

switch(item.getItemId()){
case R.id.about:
new AlertDialog.Builder(this).setTitle("Credit")
.setMessage("THIS APP IS DEVELOPED BY MANJIT SAHA")
.setPositiveButton("Close" , null).show();

return true;

default:
return false;
}
}


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


lv = findViewById(R.id.customListView);
sv = findViewById(R.id.searchView);

sql = this.openOrCreateDatabase("inventory", MODE_PRIVATE, null);
sql.execSQL("CREATE TABLE IF NOT EXISTS item_list (Id INTEGER PRIMARY KEY," +
"item_name VARCHAR," +
"sac_hsn VARCHAR," +
"price INT," +
"stock INT)");


show();



lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext() , UpdateItems.class);
i.putExtra("Id" , Integer.parseInt(midArr.get(position).toString()));
startActivity(i);

}
});

lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

delete(Integer.parseInt(midArr.get(position).toString()));
lv.setSelection(position);

return true;
}
});


}


}


class myAdapter extends ArrayAdapter{

Context context;
ArrayList ridArr = new ArrayList();
ArrayList ritemNameArr = new ArrayList();
ArrayList rsacPriceStockArr = new ArrayList();

myAdapter(Context c , ArrayList id , ArrayList name , ArrayList sps){
super(c , R.layout.row , R.id.idTv , id);

this.context = c;
this.ridArr = id;
this.ritemNameArr = name;
this.rsacPriceStockArr = sps;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View row = layoutInflater.inflate(R.layout.row , parent , false);

TextView idt = row.findViewById(R.id.idTv);
TextView itemname = row.findViewById(R.id.itemNameRow);
TextView sps = row.findViewById(R.id.spsRow);

idt.setText(ridArr.get(position).toString());
itemname.setText(ritemNameArr.get(position).toString());
sps.setText(rsacPriceStockArr.get(position).toString());

return row;
}


}

请不要删除或说尝试。我从昨天开始就在努力。请实现。

最佳答案

我实现了它。但我无法使用过滤适配器的适配器来实现它。我正在显示来自 sqlite 的数据。所以,我直接从sqlite获取搜索数据。

 public void search(String searchText) {
ArrayList localIdArr = new ArrayList();
ArrayList localItemNameArrr = new ArrayList();
ArrayList localSacPriceStockArrr = new ArrayList();

myAdapter localMad = new myAdapter(getApplicationContext(), localIdArr, localItemNameArrr, localSacPriceStockArrr);
Log.i("Query ", searchText);
Cursor c = sql.rawQuery("SELECT * FROM item_list WHERE item_name LIKE '%" + searchText + "%'", null);

if (c.moveToFirst()) {
do {
localIdArr.add(c.getInt(0));
localItemNameArrr.add(c.getString(1));
localSacPriceStockArrr.add("Sac/Hsn : " + c.getString(2) + " "
+ "Price : " + c.getInt(3) + " "
+ "Stock : " + c.getInt(4));
//(id ,item_name , sac_hsn , price , stock)
} while (c.moveToNext());
}


lv.setAdapter(localMad);
c.close();
}

之后,我在 searchView queryTextListener 上调用此函数并传递查询参数。它的作用就像黄油一样。

关于java - 无法在我的自定义 ListView 中实现搜索 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61853910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com