gpt4 book ai didi

java - 如何从我的自定义适配器更新 ListView

转载 作者:行者123 更新时间:2023-12-02 11:31:51 25 4
gpt4 key购买 nike

我只是想更新我的 ListView,但我不能。我不知道是什么。我做错了什么?我猜想我创建的适配器缺少一些东西来返回我可以处理的真实适配器。

Home.java(MainActivity)

    protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
MenuItem item = navigation.getMenu().findItem(R.id.navigation_home);
item.setCheckable(true);
item.setChecked(true);
BoxStore boxStore = AppMain.getBoxStore();
equipamentoBox = boxStore.boxFor(Equipamento.class);
lancamentoBox = boxStore.boxFor(Lancamento.class);

loadObjects();

////FOCUS HERE/////------------------------------
List<Equipamento> equipamentos = new ArrayList<>();
EquipamentoAdapter adapter;adapter = new EquipamentoAdapter(this, equipamentos);
listEquipamentos = (ListView) findViewById(R.id.listEquipamentos);
listEquipamentos.setAdapter(adapter);

registerForContextMenu(listEquipamentos);



}

EquipamentoAdapter.JAVA

public class EquipamentoAdapter extends ArrayAdapter<Equipamento> {
private final Activity context;
private final List<String> idArray = new ArrayList<String>();
private final List<String> qtdArray = new ArrayList<String>();
private final ArrayList<String> nomeArray = new ArrayList<String>();
private List<Equipamento> equipamentos = new ArrayList<>();

public EquipamentoAdapter(Activity context, List<Equipamento> equipamentos) {
super(context, R.layout.listview_row, equipamentos);
for (Iterator iterator = equipamentos.iterator(); iterator.hasNext(); ) {
Equipamento equipamento = (Equipamento) iterator.next();
this.idArray.add(Integer.toString((int)equipamento.getId()));
this.qtdArray.add(Integer.toString(equipamento.getQuantidade()));
this.nomeArray.add(equipamento.getNome());
}
this.context = context;
this.equipamentos = equipamentos;
}

public void callDialogTransaction(Equipamento equipamento) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(getContext());
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

View mView = inflater.inflate(R.layout.dialog_lancamento,null);
TextView title = (TextView) mView.findViewById(R.id.txtTitle);
final EditText quantidade = (EditText) mView.findViewById(R.id.edtQtd);
final EditText Observacao = (EditText) mView.findViewById(R.id.edtObs);
Button addTransaction = (Button) mView.findViewById(R.id.btnAddTranD);
title.setText(equipamento.getNome());

addTransaction.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
if(!quantidade.getText().toString().isEmpty()){
Toast.makeText(getContext(), "Success!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "Erro. Fill everything.", Toast.LENGTH_SHORT).show();
}
}
});

mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
}

public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.listview_row,null,true);

//this code gets references to objects in the listview_row.xml file
TextView txtQtd,txtName;
txtQtd = (TextView) rowView.findViewById(R.id.txtQtd);
txtName = (TextView) rowView.findViewById(R.id.txtName);
final ImageButton btnAddTransaction = (ImageButton) rowView.findViewById(R.id.btnAddTransaction);

//this code sets the values of the objects to values from the arrays
txtQtd.setText(qtdArray.get(position));
txtName.setText(nomeArray.get(position));

btnAddTransaction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Equipamento equipamento = equipamentos.get(position);
callDialogTransaction(equipamento);
Animation animation = new AlphaAnimation(1.0f,0.8f);
animation.setDuration(100);
btnAddTransaction.startAnimation(animation);
}
});
return rowView;
}

}

我读到我可以尝试使用adapter.notifyDataSetChanged();但它不起作用。另外,我尝试将其添加到 EquipamentoAdapter.java 上,并在需要刷新时从 MainActivity 进行调用,但它也不起作用。我不知道为什么。一切似乎都正确。

public void refreshData(){
this.equipamentos.clear();
for(Equipamento equipamento : equipamentoBox.getAll()){
this.equipamentos.add(equipamento);
}

notifyDataSetChanged();
}

最佳答案

我建议进行以下更改:

  1. 直接从getView内的List引用equipamento对象,这样getView函数就变成了

    public View getView(final int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.listview_row,null,true);

    //this code gets references to objects in the listview_row.xml file
    TextView txtQtd,txtName;
    txtQtd = (TextView) rowView.findViewById(R.id.txtQtd);
    txtName = (TextView) rowView.findViewById(R.id.txtName);
    final ImageButton btnAddTransaction = (ImageButton) rowView.findViewById(R.id.btnAddTransaction);

    //this code sets the values of the objects to values from the arrays

    Equipamento equipamento = equipamentos.get(position);

    txtQtd.setText(String.valueOf(equipamento.getId()));
    txtName.setText(String.valueOf(equipamento.getQuantidade()));

    btnAddTransaction.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    Equipamento equipamento = equipamentos.get(position);
    callDialogTransaction(equipamento);
    Animation animation = new AlphaAnimation(1.0f,0.8f);
    animation.setDuration(100);
    btnAddTransaction.startAnimation(animation);
    }
    });
    return rowView;

    }

  2. 使用 getCount 方法设置项目计数

    公共(public) int getCount(){ 返回设备大小();}

使用这些,调用 notifyDataSetChanged(); 应该更新列表,而无需重新初始化适配器。

关于java - 如何从我的自定义适配器更新 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49240116/

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