我遇到了Android问题
我不知道如何添加ArrayAdapter
或ListAdapter
来显示我的2 dim数组。
我得到了:
ArrayList<ArrayList<String>> laktoseList;
laktoseList = new ArrayList<ArrayList<String>>();
laktoseList.add(new ArrayList<String>());
laktoseList.get(0).add("Milch");
laktoseList.get(0).add("15");
laktoseList.add(new ArrayList<String>());
laktoseList.get(1).add("Jogurht");
laktoseList.get(1).add("11");
laktoseList.add(new ArrayList<String>());
laktoseList.get(2).add("Woot");
laktoseList.get(2).add("19.5");
ListView listViewName = (ListView) findViewById(R.id.listName);
ListView listViewGramm = (ListView) findViewById(R.id.listGramm);
MyCustomArrayAdapter nameAdapter = new MyCustomArrayAdapter(this, R.id.listName, laktoseList.get(0));
MyCustomArrayAdapter grammAdapter = new MyCustomArrayAdapter(this, R.id.listGramm, laktoseList.get(1));
到现在为止?我得到了我的2 dim数组,用于Item的名称和gramm。
我在ADT中创建了2 ListViews
,这应该进入。
但我不知道如何为ArrayList<ArrayList>
编写适配器。 :(
将值设置为List。
由于
修改
但我需要2个值。这是一张图片pic-upload.de/view-23169548/app.jpg.html我需要说第一个列表的名称和第二个列表中的语法
答案 0 :(得分:1)
您需要使用Hashmap存储此类数据,如此初始化。
HashMap<String, String> hashMap = new HashMap<String, String>();
将数据插入到hashmap中,例如
hashMap.put("key", value);
创建一个可以容纳hashmap的数组列表,
ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
将哈希映射插入arrayList
arrayList.add(hashMap);
使用简单的适配器来保存数组
int[] ids = {android.R.id.yourtextid android.R.id.yourtextid};
String[] keys = {Create_key, Create_KEY};
SimpleAdapter adapter = new SimpleAdapter(this, arrayList, android.R.layout.simple_list_item_2, keys, ids);
最后,只使用一个listview
listview.setAdapter(adapter);
答案 1 :(得分:1)
您应该使用一个列表视图,并且应该为列表创建pojo。我写信给你:)
您的活动(您可以使用片段):
public class YourActivity extends Activity {
ArrayList<Item> laktoseList = new ArrayList<Item>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//add your objects to list
laktoseList.add(new Item("Milch", "15"));
laktoseList.add(new Item("Jogurht", "11"));
laktoseList.add(new Item("Woot", "19.5"));
ListView listView = (ListView) findViewById(R.id.your_listview);
ItemListAdapter adapter = new ItemListAdapter(this, list_item_laktose, laktoseList);
listView.setAdapter(adapter);
}
}
自定义适配器:
public class ItemListAdapter extends ArrayAdapter<Item> {
private ArrayList<Item> mDatas;
private Context mContext;
private LayoutInflater mInflater;
private int mListItemResourceID;
public ItemListAdapter(
Context context, int listItemResourceId, ArrayList<Item> items) {
super(context, listItemResourceId, items);
mDatas = items;
mContext = context;
mInflater = LayoutInflater.from(mContext);
mListItemResourceID = listItemResourceId;
}
class ViewHolder {
TextView mNameTextView;
TextView mGrammTextView;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final Item item = mDatas.get(position);
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(mListItemResourceID, parent, false);
holder = new ViewHolder();
holder.mNameTextView = (TextView) convertView.findViewById(R.id.textview_name);
holder.mGrammTextView = (TextView) convertView.findViewById(R.id.textview_gramm);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (item != null) {
holder.mNameTextView.setText(item.getName());
holder.mGrammTextView.setText(item.getGramm());
}
return convertView;
}
}
POJO:
public class Item {
private String name;
private String gramm;
//you can also add different variables
public Item(String name, String gramm) {
this.name = name;
this.gramm = gramm;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGramm() {
return gramm;
}
public void setGramm(String gramm) {
this.gramm = gramm;
}
}
list_item_laktose:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textview_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview_gramm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
答案 2 :(得分:0)