所以,我想从我的Listview自定义元素,我做了一个自定义适配器。元素没有显示与ListView的自定义适配器
public class elementList extends BaseAdapter {
Context context;
String[] data;
private static LayoutInflater inflater = null;
public elementList(Context context, String[] data) {
this.context = context;
this.data = data;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return data.length;
}
@Override
public Object getItem(int position) {
return data[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if(vi == null)
vi = inflater.inflate(R.layout.task, null);
TextView text = (TextView)vi.findViewById(R.id.title);
Log.d("TAG", "DA");
text.setText(data[position]);
return vi;
}
}
这是我的主要活动
private void showTasks(){
final ArrayList<String> taskList = new ArrayList<String>();
ListView listView = (ListView)findViewById(R.id.listView);
final List<String> tasks = new ArrayList<String>(taskList);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tasks);
final elementList element = new elementList(this, tasks.toArray(new String[0]));
listView.setAdapter(element);
FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference adress = database.getReference("users/" + user.getUid() + "/tasksList");
adress.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
try{
if(dataSnapshot.getValue() != null){
Map<String, String> value = (Map<String, String>)dataSnapshot.getValue();
for (Map.Entry<String, String> entry : value.entrySet()){
tasks.add(entry.getValue());
Log.d("VALOARE", entry.getValue());
element.notifyDataSetChanged();
}
}
}
catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
我不明白为什么,如果我用我的适配器(元素),它不会出现在我的名单,但如果我用它工作正常arrayadapter。
在我使用arrayadapter所述第一画面和所述第二元件。我想提到它不会崩溃或给出任何错误。
我发现'静态'布局充气器可疑。另请查看https://possiblemobile.com/2013/05/layout-inflation-as-intended/了解如何将父视图传递给'inflate'方法。 – frozenkoi
支持'elementList''适配器的数组永远不会被更改/更新。你可以在任务清空时将'tasks'' List'的数组拷贝一次,但是之后再也不要改变这个数组。更新'List'不会对该数组做任何事情。为什么不让你自定义的'Adapter'带一个'List'来代替,所以你可以像'ArrayAdapter'一样更新它? –
分享你的task.xml布局 –