1
我試圖根據標題,作者或ISBN的搜索查詢製作書籍的動態列表視圖。動態列表視圖Android
我可以顯示一個項目,但無論出於何種原因,我看不到任何與搜索查詢匹配的其他項目。
Main方法
// Set up search array
int i = 0;
String searchEntries[] = InventoryAdapter.getTitleAndAuthorByISBN(isbn);
searchArray.add(new InventoryItem(searchQuery, searchEntries));
// add data in custom adapter
adapter = new CustomAdapter(this, R.layout.list, searchArray);
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
CustomAdapter
public class CustomAdapter extends ArrayAdapter<InventoryItem> {
Context context;
int layoutResourceId;
LinearLayout linearMain;
ArrayList<InventoryItem> data = new ArrayList<InventoryItem>();
public CustomAdapter(Context context, int layoutResourceId,
ArrayList<InventoryItem> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
linearMain = (LinearLayout) row.findViewById(R.id.lineraMain);
InventoryItem myItem = data.get(position);
for (int j = 0; j < myItem.getDetails().length; j++) {
TextView label = new TextView(context);
label.setText(myItem.details[j]);
linearMain.addView(label);
}
}
return row;
}
}
InventoryItem
public class InventoryItem {
public String query;
public String details[];
public InventoryItem(String query, String[] details) {
super();
this.query = query;
this.details = details;
}
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
public String[] getDetails() {
return details;
}
public void setDetails(String[] details) {
this.details = details;
}
}
list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lineraMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
</LinearLayout>
主要方法XML
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="95"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:overScrollMode="always"
android:isScrollContainer="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="outsideInset"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
</ScrollView>
</LinearLayout>
任何想法我做錯了嗎?
在getView編輯的主要區別是,你拿出for循環,但如果我取出for循環,我在label.setText中添加了什麼(myItem.details [j]); - 沒有「j」沒有這個循環? – erad
@ user3152800是的,沒有必要。這就是爲什麼。你可以使用'位置' – Raghunandan
@ user3152800和一個很大的不滾動列表視圖。刪除滾動視圖 – Raghunandan