我使用来自Internet XML Source的 StringBuilder 填充了 ListView 。
使用此代码,listView仅填充一个String,但我想按元素填充listview: getIdLine 和 getTimeLeft (使用 CustomAdapter )用于自定义分隔字符串中listView项的布局。
如何实现这一目标?
已编辑的代码
FragmentActivity.class
private ListView listViewEMT;
private ArrayList<HashMap<String, String>> yourList;
... AsyncTask
protected void onPostExecute(String string) {
super.onPostExecute(string);
CustomAdapter adapter = new CustomAdapter(getActivity(), yourList);
listViewEMT.setAdapter(adapter);
this.progressDialog.dismiss();
}
/** RSS HANDLER CLASS */
class RSSHandler extends DefaultHandler {
StringBuffer chars;
private Arrival currentArrival;
RSSHandler() {
this.currentArrival = new Arrival();
this.chars = new StringBuffer();
}
public void characters(char[] arrc, int n, int n2) {
this.chars.append(new String(arrc, n, n2));
}
public void endElement(String string, String string2, String string3) throws SAXException {
super.endElement(string, string2, string3);
if ((string2.equalsIgnoreCase("idStop")) && (this.currentArrival.getIdStop() == null)) {
this.currentArrival.setIdStop(this.chars.toString());
}
if ((string2.equalsIgnoreCase("idLine")) && (this.currentArrival.getIdLinea() == null)) {
this.currentArrival.setIdLinea(this.chars.toString());
}
if ((string2.equalsIgnoreCase("TimeLeftBus")) && (this.currentArrival.getTimeLeft() == 0)) {
int n = Integer.valueOf((String)(this.chars.toString()));
this.currentArrival.setTimeLeft(n);
}
if (!(string2.equalsIgnoreCase("Arrive"))) return;
yourList.add((HashMap<String, String>)(currentArrival.getMap()));
this.currentArrival = new Arrival();
}
public void startElement(String string, String string2, String string3, org.xml.sax.Attributes attributes) throws SAXException {
super.startElement(string, string2, string3, attributes);
this.chars = new StringBuffer();
string2.equalsIgnoreCase("Arrive");
}
}
Arrival.class
...getters and setters
public HashMap<String, String> getMap() {
HashMap<String, String> map;
map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("KEY1", idLinea);
map.put("KEY2", String.valueOf(timeLeft));
return map;
}
CustomAdapter.class
感谢Nabin
public class CustomAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
private List<String> listString;
public CustomAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.emt_item, null);
TextView tv1 = (TextView) vi.findViewById(R.id.itemLine);
TextView tv2 = (TextView) vi.findViewById(R.id.itemTime);
HashMap<String, String> map;
map = data.get(position);
tv1.setText(map.get("KEY1"));
tv2.setText(map.get("KEY2"));
return vi;
}
}
答案 0 :(得分:1)
创建自定义适配器,如下所示:
public class ArrayAdapter extends BaseAdapter{
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
private List<String> listString;
public ArrayAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//your getView method here
}
GetView方法
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.custom, null);
TextView tv1 = (TextView) vi.findViewById(R.id.tvone);
TextView tv2 = (TextView) vi.findViewById(R.id.tvtwo);
HashMap<String, String> map = new HashMap<String, String>();
map = data.get(position);
tv1.setText(map.get("KEY1"));
tv2.setText(map.get("KEY2"));
return vi;
}
将数组列表设为:
ArrayList<HashMap<String, String>> yourList;
将 yourList 填入
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("KEY1", value1);
map.put("KEY2", value2);
yourList.add(map);
在制作自定义适配器的对象时
CustomAdapter adapter = new CustomAdapter(YourActivity.this, yourList);
list.setAdapter(adapter);
对于列表,您可以
list = (ListView) getView().findViewById(android.R.id.list);