Android客户阵列适配器不显示数据

Android Customer Array Adapter does not display data

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

我尝试实现一个 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;
}

}

最终结果应该是一个注释列表以及用户偏好和 location/reminder。 请参阅下面在 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());
}

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

您没有将您提供给适配器的列表存储到本地字段。 在这里,您通过适配器构造函数传递 ArrayList<Note> objects,但没有将其保存到本地某些商店。

  • 所以,首先修改你的 Adapter 以拥有 ArrayList<Note> 的字段 并在构造函数中初始化它。
  • 其次,将适配器 getCount() 覆盖为 return 您的尺寸 列表。
  • 第三:修改你的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.

希望这能解决您的问题。