2
我試圖發送此ArrayList:收集3周的ArrayList到一個和一個自定義的ListView查看它
all1 = all.toArray(new String[all.size()]);
這是一個JSON解析這三個數組列表的總和:
for (int j = 0; j < itemNames.size(); j++) {
all.add(brandNames.get(j) + " " + itemNames.get(j) + " " + Carbs.get(j));
}
自定義列表視圖,但無法設置適配器。
我有一個自定義列表視圖如下(search_list_view):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin">
<!-- layout child -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Edit button -->
<Button
style="?android:attr/buttonStyleSmall"
android:text="@string/add"
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
</RelativeLayout>
<!-- Text view for main text -->
<TextView
android:id="@+id/brandName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Item Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
我明白我需要一個定製的適配器到目前爲止,我有這一點,但它不工作:
private class FoodAdapter extends ArrayAdapter<Food> {
public FoodAdapter(Context context, List<Food> objects) {
super(context, 0, objects);
}
//inflate view layout_list_item
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView =
getLayoutInflater().inflate(R.layout.search_list_item, parent, false);
}
//link buttons to views within layout_list_item
Button btnAdd = (Button) convertView.findViewById(R.id.btnAdd);
TextView txtMemo = (TextView)
convertView.findViewById(R.id.brandName);
return convertView;
我型號如下:
public class Food implements Serializable {
//set variables
private Date date;
private String text;
private boolean fullDisplayed;
private static DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyy 'at' hh:mm aaa");
//memo date
public Food() {
this.date = new Date();
}
//create Food
public Food(long time, String text) {
this.date = new Date(time);
this.text = text;
}
//get time
public long getTime() {
return date.getTime();
}
//set text
public void setText(String text) {
this.text = text;
}
//get text
public String getText() {
return this.text;
}
//get shortened version of text for not full displayed
public String getShortText() {
String temp = text.replaceAll("\n", " ");
if (temp.length() > 25) {
return temp.substring(0, 25) + "...";
} else {
return temp;
}
}
//set full displayed
public void setFullDisplayed(boolean fullDisplayed) {
this.fullDisplayed = fullDisplayed;
}
//set flag for if is full displayed
public boolean isFullDisplayed() {
return this.fullDisplayed;
}
//set a toString method for returning text.
@Override
public String toString() {
return this.text;
}
}
什麼不工作?它不顯示屏幕上的任何東西嗎?它不填充數據或..的項目?!它甚至進入getView()? – AndreiBogdan