這是我的 MainActivity,我在其中定義了 dataRefresh 方法,該方法在從資料庫中插入或洗掉資料時重繪 到配接器
public class MainActivity extends AppCompatActivity {
EditText phone, name;
ListView list;
MyDatabase db;
CustomAdapter ad;
ArrayList<contact> cont;
Button add;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=findViewById(R.id.list_item);
db=new MyDatabase(this);
cont=new ArrayList<>();
cont=db.showData();
ad=new CustomAdapter(this,cont;
list.setAdapter(ad);
}
public void dataRefresh(){
cont.clear();
cont.addAll(db.showData());
ad.notifyDataSetChanged();
}
上面是一個dataRefresh方法
這是我的配接器類,我在其中呼叫洗掉方法從資料庫資料中洗掉資料
public class CustomAdapter extends ArrayAdapter {
ArrayList<contact> phnecontact;
MyDatabase db;
LinearLayout horizon;
Context context;
public CustomAdapter(@NonNull Context context, ArrayList<contact>
phonecontact) {
super(context,R.layout.customlayout,phonecontact);
this.context = context;
this.phnecontact = phonecontact;
}
public contact getItem(int position) {
return phnecontact.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public View getView(int position, View i, ViewGroup parent) {
View view =
LayoutInflater.from(context).inflate(R.layout.customlayout, parent,
false);
contact s1 = phnecontact.get(position);
db=new MyDatabase(context);
TextView name=view.findViewById(R.id.cmname);
horizont=view.findViewById(R.id.horizontal);
TextView number=view.findViewById(R.id.cmphone);
ImageView sms=view.findViewById(R.id.message);
ImageView call=view.findViewById(R.id.phone);
ImageView photo=view.findViewById(R.id.cmphoto);
number.setText(String.valueOf(s1.getPhone()));
name.setText(s1.getName());
horizont.setOnLongClickListener(v -> {
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle("Are you sure to delete");
builder.setPositiveButton("yes", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int i =db.Deletioncontact(s1.getPhone());
我想在 if 條件之后呼叫 Mainactivity 的 dataRefresh 方法
if(i==1)
{
Toast.makeText(context, "contact is deleted",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(context, "contact not deleted",
Toast.LENGTH_SHORT).show();
}
}
});
如果我通過 Mainactivity 的 make 物件呼叫此方法,則我的應用程式已崩潰
uj5u.com熱心網友回復:
除了呼叫方法之外,mainActivity還有一些可以改進的地方。
您不必傳入context配接器,要使用內部getView()方法,您可以從中獲取背景關系parent viewGroup
@Override
public View getView(int position, View i, ViewGroup parent) {
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.customlayout, parent,
false);
您可以將您的所有clickListeners和longClickListeneners邏輯移動到您的activity, 并用于interface觸發這些方法。通過這種方式,將讓您的配接器類松耦合的activity,你可以使用這個adapter在另一個activity或fragment過。
clickInterface在您的配接器類中創建
public interface ClickListeners {
public void onItemlongClickListener(int phone);
}
現在interface在您的活動中覆寫它
public class MainActivity extends AppCompatActivity implements CustomAdapter.ClickListener{
//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
ad = new CustomAdapter(this, cont); // pass interface instance to the adapter
//
}
@Override
public void onItemlongClickListener(int phone) {
// add your dialog confirmation
// and item deletion code here
}
}
更改CustomAdaper建構式以獲取介面實體
public CustomAdapter(ClickListeners clickListeners, ArrayList<contact>
phonecontact) {
super(context,R.layout.customlayout,phonecontact);
this.clickListeners = clickListeners;
this.phnecontact = phonecontact;
}
onItemlongClickListener當用戶長按專案時觸發
horizon.setOnLongClickListener(v -> {
clickListeners.onItemlongClickListener(s1.getPhone()); // call interface method
}
uj5u.com熱心網友回復:
**在你的 onclick 方法中試試這個 **
public void onClick(DialogInterface dialog, int which) {
int i =db.Deletioncontact(s1.getPhone());
Iterator<Integer> itr = numbers.iterator();
while(itr.hasNext()) {
Phone phone = itr.next();
if (phone == s1.getPhone()) {
s1.remove(phone);
}
}
notifyDataSetChange();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/311559.html
標籤:安卓
