【发布时间】:2015-11-06 10:18:35
【问题描述】:
登录后我需要显示一个列表,其中包含用户名及其描述。
我正在使用自定义的列表视图,它没有显示任何错误但列表没有显示。
我尝试了 stackoverflow 中的所有解决方案,但没有显示列表。
数据在服务器中以正确的 JSON 格式显示。
从服务器获取。
@Override
protected descusers doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("username", user.username));
dataToSend.add(new BasicNameValuePair("descCrip", user.descCrip));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams,
CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams,
CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS
+ "fetchdesc.php");
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse;
httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
// JSONObject jObject = new JSONObject(result);
//JSONObject jsonObject = new JSONObject(result);
//JSONArray jsonArray = jsonObject.getJSONArray("");
JSONArray json = new JSONArray(result);
for (int i=0;i<json.length();i++)
{
JSONObject e=json.getJSONObject(i);
String username = e.getString("username");
String descCrip = e.getString("descCrip");
dusers.add(new descusers(username,descCrip));
}
descAdapter = new DescAdapter(descStore.this,dusers);
mViews.lists.setAdapter(descAdapter);
/* if (jObject.length() != 0){
Log.v("happened", "2");
String name = jObject.getString("name");
int age = jObject.getInt("age");
returnedUser = new User(name, age, user.username,
user.password);
}*/
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
自定义适配器:
public class DescAdapter extends BaseAdapter{
private final descStore ds;
private ArrayList<descusers> dusers;
public DescAdapter(descStore ds, ArrayList<descusers> dusers) {
this.ds = ds;
this.dusers = dusers;
}
@Override
public int getCount() {
return dusers.size();
}
@Override
public Object getItem(int position) {
return dusers.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
descusers du = dusers.get(position);
String username = du.username;
String descCrip=du.descCrip;
View view= LayoutInflater.from(ds).inflate(R.layout.customlist,null);
TextView uname = (TextView)view.findViewById(R.id.username);
TextView desc = (TextView)view.findViewById(R.id.description);
uname.setText(username);
desc.setText(descCrip);
return view;
}
}
自定义列表.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="16dp">
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="username"
android:textColor="#ff6aa9ff"
android:textSize="20dp" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/username"
android:text="Suggest a food to try"
android:textColor="#ff06010b"
android:textSize="16dp" />
listview.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffffff"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.snatarajan.fooddoof.loginregister.descStore">
<EditText
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/post"
android:layout_toStartOf="@+id/post"
android:hint="Suggest a food to try" />
<Button
android:id="@+id/post"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="post" />
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_above="@+id/post"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@android:color/black" />
<ListView
android:id="@+id/lists"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/post"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
【问题讨论】:
标签: android xml android-layout listview android-listview