【问题标题】:Android Customer Array Adapter does not display dataAndroid自定义Arrayadapter不显示数据
【发布时间】:2020-08-03 08:41:23
【问题描述】:

您好 StackOverflow 社区!我需要您的帮助来理解以下行为:

我尝试实现一个 ListView,每个视图都遵循以下布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLWLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="100">


<TextView
    android:id="@+id/noteText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="66.6" />

<LinearLayout
    android:id="@+id/linearVLWLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="33.3"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textcolor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/reminder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textcolor" />

    <TextView
        android:id="@+id/location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/reminder" />

    <TextView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/location" />
</LinearLayout>

现在,当我将元素分配给列表时,我正在使用以下适配器:

public class MyAdapter extends ArrayAdapter<Note> {

private Context mContext;
private int mResource;

public MyAdapter(@NonNull Context context, int resource, ArrayList<Note> objects) {
    super(context, resource);
    mContext =context;
    mResource=resource;
}

public View getView(int position, View convertView, ViewGroup parent){

    String text = getItem(position).getText();
    String color = getItem(position).getColor();
    String location = getItem(position).getLocation();
    String reminder = getItem(position).getReminder();
    String image = getItem(position).getImage();

    Note note = new Note(text,color,location,reminder,image);

    LayoutInflater inflater = LayoutInflater.from(mContext);
    convertView=inflater.inflate(mResource,parent,false);

    TextView nttxt = (TextView) convertView.findViewById(R.id.noteText);
    TextView ntcolor = (TextView) convertView.findViewById(R.id.textcolor);
    TextView ntrem = (TextView) convertView.findViewById(R.id.reminder);
    TextView ntlocat = (TextView) convertView.findViewById(R.id.location);
    TextView ntimg = (TextView) convertView.findViewById(R.id.image);

    nttxt.setText(text);
    ntcolor.setText(color);
    ntrem.setText(reminder);
    ntlocat.setText(location);
    ntimg.setText(image);
    Log.i("Convert",convertView.toString());
    Log.i("Text",nttxt.toString());
    return convertView;
}

}

最终结果应该是附有用户偏好和位置/提醒的笔记列表。 请参阅下面对 OnCreate 方法所做的列表分配:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_explorer);
        FloatingActionButton myFab = this.findViewById(R.id.fabAddNote);
        myFab.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intentNoteEditor = new Intent(getApplicationContext(), NoteEditor.class);
            startActivity(intentNoteEditor);
        }
    });
        ListView notesList = (ListView) findViewById(R.id.listviewNotes);
        Note note1 = new Note("Note1","Col1","Reminder1","Location1","Image1");
        Note note2 = new Note("Note2","Col2","Reminder2","Location2","Image2");
        Note note3 = new Note("Note3","Col3","Reminder3","Location3","Image3");
        Note note4 = new Note("Note4","Col4","Reminder4","Location4","Image4");
        ArrayList<Note> notesArray = new ArrayList<>();
        notesArray.add(note1);
        notesArray.add(note2);
        notesArray.add(note3);
        notesArray.add(note4);
        Log.i("Notes",note1.getText().toString());
        MyAdapter adapter = new MyAdapter(this,R.layout.list_item,notesArray);
        notesList.setAdapter(adapter);
        Log.i("Adapter",adapter.getContext().toString());
}

我做错了什么? 任何建议将不胜感激。 提前谢谢!

【问题讨论】:

    标签: android android-layout android-listview android-arrayadapter


    【解决方案1】:

    您没有将您提供给适配器的列表存储到本地字段。 在这里,您通过适配器构造函数传递 ArrayList&lt;Note&gt; objects,但没有将其保存到本地存储。

    • 所以,首先将您的适配器修改为具有ArrayList&lt;Note&gt; 的文件 并在构造函数中对其进行初始化。
    • 其次,覆盖适配器getCount() 以返回您的大小 列表。
    • 第三:修改你的getView(),获取当前的Note对象 在列表中的位置
    public class MyAdapter extends ArrayAdapter<Note> {
    
    private Context mContext;
    private int mResource;
    private ArrayList<Note> mNotes;
    
        public MyAdapter(@NonNull Context context, int resource, ArrayList<Note> objects) {
            super(context, resource);
            mContext = context;
            mResource = resource;
            mNotes = objects;
        }
    
        @Override
        public int getCount() {
            return mNotes.size();
        }
    
        public View getView(int position, View convertView, ViewGroup parent){
    
            String text = mNotes.get(position).getText(); 
            String color = mNotes.get(position).getColor();
            String location = mNotes.get(position).getLocation();
            String reminder = mNotes.get(position).getReminder();
            String image = mNotes.get(position).getImage();
    
           // ....  continue the rest of code.
    

    希望这能解决您的问题。

    【讨论】:

    • 谢谢你,@Zain!这解决了我的问题。
    猜你喜欢
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多