如何从Firebase数据库读取数据,并使用ArrayAdapter在ListView中显示多个字段。

huangapple go评论130阅读模式
英文:

How to Read Data from Firebase Database and Display Multiple Fields in ListView with ArrayAdapter

问题

以下是你提供的代码部分的翻译:

public class SearchModel {

    public String make;
    private String model;
    private String stock;

    public SearchModel() {}

    public String getMake() {
        return this.make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getStock() {
        return stock;
    }

    public void setStock(String stock) {
        this.stock = stock;
    }
}
public class MainActivity extends AppCompatActivity {

    private DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("users").child("user1");
    private ListView mListView;
    private TextView tv_make;
    private TextView tv_model;
    private TextView tv_stock;

    private ArrayList<String> list = new ArrayList<>();
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        tv_make = (TextView) findViewById(R.id.tv_list_item_make);
        tv_model = (TextView) findViewById(R.id.tv_list_item_model);
        tv_stock = (TextView) findViewById(R.id.tv_list_item_stock);

        databaseReference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
                String value = snapshot.child("make").getValue(String.class);
                list.add(value);

                mListView = (ListView) findViewById(R.id.lv_searches);
                adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
                mListView.setAdapter(adapter);
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot snapshot) {
            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
            }
        });

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
}
<!-- content.main.xml -->
<ListView
    android:id="@+id/lv_searches"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</ListView>
<!-- list_item.xml -->
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="10dp">

    <TextView
        android:id="@+id/tv_list_item_make"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_list_item_model"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

    <TextView
        android:id="@+id/tv_list_item_stock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

以上是你提供的代码部分的翻译内容。如有问题请随时提问。

英文:

I have spent the better half of the day trying to figure out how to read data from my firebase database and display multiple fields in a ListView via an ArrayAdapter. After finally getting my ListView to display one piece of data in one textview, I am stuck trying to display two other fields.

In my MainActivity class I used the android.R.layout.simple_list_item_1 in the Adapter constructor because I was told that the textViewResourceID argument must only contain one TextView. So to reiterate my problem: I can not figure out how to display multiple fields with an array adapter. This app currently only displays the make field. Thanks so much in advance, I only started android development about a week ago! Here is my code below:

SearchModel.java

public class SearchModel {
public String make;
private String model;
private String stock;
public SearchModel() {}
public String getMake() {
return this.make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getStock() {
return stock;
}
public void setStock(String stock) {
this.stock = stock;
}
}

MainActivity.java

(I omitted the irrelevant bottom section of the class after onCreate())

public class MainActivity extends AppCompatActivity {
private DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child(&quot;users&quot;).child(&quot;user1&quot;);
private ListView mListView;
private TextView tv_make;
private TextView tv_model;
private TextView tv_stock;
private ArrayList&lt;String&gt; list = new ArrayList&lt;&gt;();
private ArrayAdapter&lt;String&gt; adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tv_make = (TextView) findViewById(R.id.tv_list_item_make);
tv_model = (TextView) findViewById(R.id.tv_list_item_model);
tv_stock = (TextView) findViewById(R.id.tv_list_item_stock);
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
String value = snapshot.child(&quot;make&quot;).getValue(String.class);
list.add(value);
mListView = (ListView) findViewById(R.id.lv_searches);
adapter = new ArrayAdapter&lt;&gt;(getApplicationContext(), android.R.layout.simple_list_item_1, list);
mListView.setAdapter(adapter);
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, &quot;Replace with your own action&quot;, Snackbar.LENGTH_LONG)
.setAction(&quot;Action&quot;, null).show();
}
});
}

Here is the ListView in content.main.xml:

&lt;ListView
android:id=&quot;@+id/lv_searches&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;/ListView&gt;

And here is the list_item.xml which is supposed to sit inside my ListView. This is where I would like to display the three String values (make, model, stock) in an instance of SearchModel.java.

&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;100dp&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:padding=&quot;10dp&quot;&gt;
&lt;TextView
android:id=&quot;@+id/tv_list_item_make&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;50dp&quot;
app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/tv_list_item_model&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;40dp&quot;
app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/tv_list_item_stock&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
app:layout_constraintRight_toRightOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

This is my firebase database layout: 如何从Firebase数据库读取数据,并使用ArrayAdapter在ListView中显示多个字段。

答案1

得分: 0

以下是翻译好的部分:

public class CustomAdapter extends BaseAdapter {

    private static ArrayList<SearchModel> searchArrayList;
    private LayoutInflater mInflater;
    private Context mContext;
    private int lastPosition;

    /**
     * Holds elements in a view
     */
    static class ViewHolder {
        TextView make;
        TextView model;
        TextView stock;
    }

    public CustomAdapter(Context context, ArrayList<SearchModel> results) {
        searchArrayList = results;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return searchArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return searchArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String make = searchArrayList.get(position).getMake();
        String model = searchArrayList.get(position).getModel();
        String stock = searchArrayList.get(position).getStock();

        final View result;

        ViewHolder holder;

        if (convertView == null) {

            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.list_item, parent, false);
            holder = new ViewHolder();
            holder.make = (TextView) convertView.findViewById(R.id.tv_list_item_make);
            holder.model = (TextView) convertView.findViewById(R.id.tv_list_item_model);
            holder.stock = (TextView) convertView.findViewById(R.id.tv_list_item_stock);

            result = convertView;
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            result = convertView;
        }

        Animation animation = AnimationUtils.loadAnimation(mContext,
                (position > lastPosition) ? R.anim.load_down_anim : R.anim.load_up_anim);
        result.startAnimation(animation);
        lastPosition = position;

        holder.make.setText(make);
        holder.model.setText(model);
        holder.stock.setText(stock);

        return convertView;
    }
}
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
            String make = snapshot.child("make").getValue(String.class);
            String model = snapshot.child("model").getValue(String.class);
            String stock = snapshot.child("stock").getValue(Long.class).toString();
            SearchModel searchModel = new SearchModel(make, model, stock);
            searchList.add(searchModel);

            CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), searchList);

            mListView = (ListView) findViewById(R.id.lv_searches);
            mListView.setAdapter(customAdapter);
        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot snapshot) {
        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        }
    });
}
英文:

after two days of being stuck I was able to answer my own question! Hopefully this will help future beginners stuck trying to display a custom view within an ArrayAdapter. Basically, I created a custom ArrayAdapter class which could hold three TextViews inside of it.

I am in no means an expert and will not be able to explain this as well as others, so instead, I will paste a link to the two video tutorials that I followed and had success with.

Android Beginner Tutorial #8 - Custom ListView Adapter For Displaying Multiple Columns
https://www.youtube.com/watch?v=E6vE8fqQPTE&amp;t=392s

Android Beginner Tutorial #9 - Custom ListView Adapter [With Loading Animation]
https://www.youtube.com/watch?v=SApBLHIpH8A

Here is my updated code below:

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {
private static ArrayList&lt;SearchModel&gt; searchArrayList;
private LayoutInflater mInflater;
private Context mContext;
private int lastPosition;
/**
* Holds elements in a view
*/
static class ViewHolder {
TextView make;
TextView model;
TextView stock;
}
public CustomAdapter(Context context, ArrayList&lt;SearchModel&gt; results) {
searchArrayList = results;
mContext = context;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return searchArrayList.size();
}
@Override
public Object getItem(int position) {
return searchArrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String make = searchArrayList.get(position).getMake();
String model = searchArrayList.get(position).getModel();
String stock = searchArrayList.get(position).getStock();
final View result;
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.make = (TextView) convertView.findViewById(R.id.tv_list_item_make);
holder.model = (TextView) convertView.findViewById(R.id.tv_list_item_model);
holder.stock = (TextView) convertView.findViewById(R.id.tv_list_item_stock);
result = convertView;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
result = convertView;
}
Animation animation = AnimationUtils.loadAnimation(mContext,
(position &gt; lastPosition) ? R.anim.load_down_anim : R.anim.load_up_anim);
result.startAnimation(animation);
lastPosition = position;
holder.make.setText(make);
holder.model.setText(model);
holder.stock.setText(stock);
return convertView;
}
}

MainActivity.java (Only the onCreate method)

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
String make = snapshot.child(&quot;make&quot;).getValue(String.class);
String model = snapshot.child(&quot;model&quot;).getValue(String.class);
String stock = snapshot.child(&quot;stock&quot;).getValue(Long.class).toString();
SearchModel searchModel = new SearchModel(make, model, stock);
searchList.add(searchModel);
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), searchList);
mListView = (ListView) findViewById(R.id.lv_searches);
mListView.setAdapter(customAdapter);
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});

huangapple
  • 本文由 发表于 2020年9月21日 00:24:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63981173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定