【问题标题】:No error but list is not displaying in customized listview没有错误,但列表未显示在自定义列表视图中
【发布时间】: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


    【解决方案1】:

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

    另外,与您的问题无关,我建议您在使用ListViews 时阅读ViewHolder 模式,以提高性能,

    补充:这是我从您的原始帖子中更改的内容:

    我不得不用一个虚拟列表创建替换你的 http&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);
    }
    

    【讨论】:

    • 添加 mViews.lists.setAdapter(descAdapter);在 onPostExecute
    • 我用对我有用的方法更新了答案。您是否在“AsyncTask”的“doInBackground”中看到数据?你在任务上调用执行吗?它是否抛出任何异常并且没有进入适配器初始化行?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多