我在HashMap中有一些小时和分钟。我的代码在这里http://pastie.org/8394598 我的输出是这样的
但是我的预期输出是
如何使用自定义列表视图添加此类型的哈希映射?我错在哪里?有人能告诉我吗?提前谢谢。
答案 0 :(得分:1)
// try this
**activity.xml**
<?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="wrap_content"
android:descendantFocusability="blocksDescendants">
<ListView
android:id="@+id/lst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:divider="@null"
android:dividerHeight="0dp"
android:smoothScrollbar="true"/>
</LinearLayout>
**list_item.xml**
<?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="wrap_content"
android:padding="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:padding="20dp"
android:background="@android:color/holo_orange_dark">
<TextView
android:id="@+id/txtHours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="30sp"
android:text="12 PM"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_weight="1"
android:padding="20dp"
android:background="@android:color/holo_orange_light">
<TextView
android:id="@+id/txtMinute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="20sp"
android:text="32"/>
<TextView
android:id="@+id/txtSecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="@android:color/white"
android:textSize="20sp"
android:text="30"/>
</LinearLayout>
</LinearLayout>
**MainActivity**
public class MainActivity extends Activity {
private ListView lst;
private CustomAdapter adapter;
ArrayList<HashMap<String,String>> data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lst = (ListView) findViewById(R.id.lst);
data = new ArrayList<HashMap<String, String>>();
HashMap<String,String> row1 = new HashMap<String, String>();
row1.put("hours","12 PM");
row1.put("minute","30");
row1.put("second","45");
HashMap<String,String> row2 = new HashMap<String, String>();
row2.put("hours","01 PM");
row2.put("minute","31");
row2.put("second","46");
HashMap<String,String> row3 = new HashMap<String, String>();
row3.put("hours","02 PM");
row3.put("minute","32");
row3.put("second","47");
HashMap<String,String> row4 = new HashMap<String, String>();
row4.put("hours","03 PM");
row4.put("minute","33");
row4.put("second","48");
HashMap<String,String> row5 = new HashMap<String, String>();
row5.put("hours","04 PM");
row5.put("minute","34");
row5.put("second","49");
data.add(row1);
data.add(row2);
data.add(row3);
data.add(row4);
data.add(row5);
adapter = new CustomAdapter(this,data);
lst.setAdapter(adapter);
lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
Toast.makeText(MainActivity.this,data.get(pos).get("hours")+":"+data.get(pos).get("minute")+":"+data.get(pos).get("second"),Toast.LENGTH_LONG).show();
}
});
}
class CustomAdapter extends BaseAdapter {
Context context;
ArrayList<HashMap<String,String>> data;
public CustomAdapter(Context context, ArrayList<HashMap<String,String>> data){
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int pos) {
return data.get(pos);
}
@Override
public long getItemId(int pos) {
return pos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.list_item,null,false);
holder.txtHours = (TextView) convertView.findViewById(R.id.txtHours);
holder.txtMinute = (TextView) convertView.findViewById(R.id.txtMinute);
holder.txtSecond = (TextView) convertView.findViewById(R.id.txtSecond);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtHours.setText(data.get(position).get("hours"));
holder.txtMinute.setText(data.get(position).get("minute"));
holder.txtSecond.setText(data.get(position).get("second"));
convertView.setTag(holder);
return convertView;
}
class ViewHolder {
TextView txtHours;
TextView txtMinute;
TextView txtSecond;
}
}
}
![enter image description here][1]
[1]: http://i.stack.imgur.com/cB2yI.png
答案 1 :(得分:0)
通过扩展SimpleAdapter类来创建自定义适配器。以下是一个示例:http://dustinbreese.blogspot.com/2009/12/creating-listview-with-alternating.html