大家好,感谢您看看这个。我使用Android的开发人员指南创建了一个动态ListView。目前,ListView活动似乎在屏幕上显示,而不需要XML文件或setContentView ...这是一个问题。我已经创建了一个动态ListView - 我如何将它连接到我的XML布局文件?
我想使用XML布局,因此我可以将其他视图添加到屏幕上,而不是将整个活动专门用于显示列表。我创建了一个XML布局,其中包含一个空白的ListView,并且我希望我的列表进入分配的空间......所以我的问题是:如何让我的ListActivity使用我的布局XML文件?
public class MainList extends ListActivity {
static SharedPreferences statusSettings;
String jsonString;
static JSONObject json;
static JSONArray arrayTest;
static int bob = 3;
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
try {
JSONObject e = arrayTest.getJSONObject(position);
Intent intent = new Intent();
intent.putExtra("clientName", e.getString("proj_name"));
intent.putExtra("clientAddress", e.getString("c_address"));
intent.putExtra("clientComments", e.getString("comments"));
intent.putExtra("clientOrder", e.getString("order"));
intent.setClass(MainList.this, ClientDetails.class);
MainList.this.startActivity(intent);
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.e("JSON", "Problem creating object from array!");
e.printStackTrace();
}
}
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return arrayTest.length();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.mainlist, null);
holder = new ViewHolder();
holder.textName = (TextView) convertView.findViewById(R.id.tvMainName);
holder.textAddress = (TextView) convertView.findViewById(R.id.tvMainAddress);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
JSONObject e;
try {
e = arrayTest.getJSONObject(position);
holder.textName.setText(e.getString("proj_name"));
holder.textAddress.setText(e.getString("c_address"));
switch (statusSettings.getInt(e.getString("order"), 0)){
case 1:
convertView.setBackgroundColor(0x00000000);
break;
case 2:
if(bob == 3){
convertView.setBackgroundColor(0xFFFF6600);
bob = 5;
}
break;
case 3:
convertView.setBackgroundColor(0xFF00CC00);
break;
case 4:
convertView.setBackgroundColor(0xFFCC0000);
break;
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
Log.e("JSON", "Couldn't put one of JSON arrays into object");
e1.printStackTrace();
}
return convertView;
}
static class ViewHolder {
TextView textName;
TextView textAddress;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
jsonString = getIntent().getExtras().getString("jsonString");
try {
json = new JSONObject(jsonString);
arrayTest = json.getJSONArray("client_list");
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.e("JSON", "Couldn't create the JSON Array");
e.printStackTrace();
}
setListAdapter(new EfficientAdapter(this));
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
bob = 3;
statusSettings = getSharedPreferences("status", 0);
setListAdapter(new EfficientAdapter(this));
}
}
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="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_weight="1">
</ListView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
的事情是,他们创造它,而无需使用任何XML布局 - 我不完全了解,但我相信它有事情做与LayoutInflater。我试过: convertView = mInflater.inflate(R.id.list,null);
而不是 convertView = mInflater.inflate(R.layout.mainlist,null);
我放置了setContentView(R.layout.test); 在我的onCreate ...但没有工作。你提供的任何帮助将不胜感激,谢谢!
是你创建的R文件是否正确? – shiraz 2012-01-30 10:22:55
是的,我没有看到它的问题,程序编译np。 – Wildblood 2012-01-30 10:23:56
顺便说一句,这里是一个很好的资源来学习Android开发,http://www.youtube.com/playlist?list = PLE953C0B85B50AB62&功能= plcp通过几个最初的视频浏览,看看是否有帮助 – shiraz 2012-01-30 10:28:15