gpt4 book ai didi

android - Android Studio 中 ListView 中的适配器

转载 作者:行者123 更新时间:2023-11-29 01:35:31 25 4
gpt4 key购买 nike

我试图通过三个类来补充控制自定义 ListView 的代码:AdapterListViewItemListViewMainActivity

到目前为止,代码仅设法通过AdapterListView 输入数据和图像(由代码插入)。在该类中,想知道如何使用应用程序添加或删除项目等其他功能。

三个项目类下面:

AdapterListView

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;

/**
* Created by LuizHMU on 2/7/15.
*/
public class AdapterListView extends BaseAdapter {

private LayoutInflater mInflater;
private ArrayList<ItemListView> itens;

public AdapterListView(Context context, ArrayList<ItemListView> itens) {
//Itens que preencheram o listview
this.itens = itens;
//responsavel por pegar o Layout do item.
mInflater = LayoutInflater.from(context);
}

/**
* Retorna a quantidade de itens
*
* @return
*/
public int getCount() {
return itens.size();
}

/**
* Retorna o item de acordo com a posicao dele na tela.
*
* @param position
* @return
*/
public ItemListView getItem(int position) {
return itens.get(position);
}

/**
* Sem implementação
*
* @param position
* @return
*/
public long getItemId(int position) {
return position;
}

public View getView(int position, View view, ViewGroup parent) {
//Pega o item de acordo com a posção.
ItemListView item = itens.get(position);
//infla o layout para podermos preencher os dados
view = mInflater.inflate(R.layout.item_list, null);

//atravez do layout pego pelo LayoutInflater, pegamos cada id relacionado
//ao item e definimos as informações.
((TextView) view.findViewById(R.id.text)).setText(item.getTexto());
((ImageView) view.findViewById(R.id.imagemview)).setImageResource(item.getIconeRid());

return view;
}
}

项目 ListView

/**
* Created by LuizHMU on 2/7/15.
*/
public class ItemListView {

private String texto;
private int iconeRid;

public ItemListView() {
}

public ItemListView(String texto, int iconeRid) {
this.texto = texto;
this.iconeRid = iconeRid;
}

public int getIconeRid() {
return iconeRid;
}

public void setIconeRid(int iconeRid) {
this.iconeRid = iconeRid;
}

public String getTexto() {
return texto;
}

public void setTexto(String texto) {
this.texto = texto;
}
}

主 Activity

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends Activity implements OnItemClickListener {

private ListView listView;
private AdapterListView adapterListView;
private ArrayList<ItemListView> itens;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//carrega o layout onde contem o ListView
setContentView(R.layout.activity_main);

//Pega a referencia do ListView
listView = (ListView) findViewById(R.id.list);
//Define o Listener quando alguem clicar no item.
listView.setOnItemClickListener(this);

createListView();
}

private void createListView() {
//Criamos nossa lista que preenchera o ListView
itens = new ArrayList<ItemListView>();
ItemListView item1 = new ItemListView("Felpudo", R.drawable.felpudo);
ItemListView item2 = new ItemListView("Felpudão", R.drawable.felpudo1);
ItemListView item3 = new ItemListView("Felpudinho", R.drawable.felpudo2);

itens.add(item1);
itens.add(item2);
itens.add(item3);

//Cria o adapter
adapterListView = new AdapterListView(this, itens);

//Define o Adapter
listView.setAdapter(adapterListView);
//Cor quando a lista é selecionada para ralagem.
listView.setCacheColorHint(Color.TRANSPARENT);
}

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//Pega o item que foi selecionado.
ItemListView item = adapterListView.getItem(arg2);
//Demostração
Toast.makeText(this, "Você Clicou em: " + item.getTexto(), Toast.LENGTH_LONG).show();
}
}

最佳答案

将此添加到您的适配器:

public void addItem(ItemListView item){
items.add(item);
notifyDatasetChanged();
}

public void removeItem(int positionToRemove){
items.remove(positionToRemove);
notifyDatasetChanged();
}

为了进一步解释发生了什么,适配器是 Controller ,是信息和在 View (ListView) 中显示信息的方式之间的中间步骤。本例中的信息通过项目列表提供给适配器,因此基本上操作列表并调用 notifyDataSetChanged() 将更改显示给用户的 View 。

希望我能帮到您。

关于android - Android Studio 中 ListView 中的适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28414075/

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