没有错误,但列表未显示在自定义列表视图中

时间:2015-08-13 12:33:24

标签: android xml android-layout listview android-listview

记录后,我需要显示一个列表,其中包含用户名及其描述。

我正在使用自定义列表视图,其中没有显示任何错误但列表未显示。

我尝试使用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;
}  
}

Customlist.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" />

1 个答案:

答案 0 :(得分:0)

我认为问题出在你的AsyncTaskdoInBackground(顾名思义......)在后台线程中执行。在列表上设置适配器需要在UI线程上完成。所以我认为在mViews.lists.setAdapter(descAdapter);(将在UI线程上执行)中移动onPostExecute调用就足够了。

另一方面,与您的问题无关,建议您在使用ViewHolder时了解ListViews模式,以提高效果,

添加:这是我在原帖中改变的内容:

我不得不用虚拟列表创建替换你的http&amp; json部分:

ArrayList<YourAdapter.descusers> dusers = new ArrayList<>();
for (int i = 0; i < 10; i++)
    dusers.add(new YourAdapter.descusers("username", "descCrip"));
descAdapter = new YourAdapter(<your_context>, dusers);

onPostExecute

@Override
protected void onPostExecute(Long aLong)
{
    super.onPostExecute(aLong);
    list.setAdapter(descAdapter);
}