【问题标题】:How to display multiple row in listview android如何在listview android中显示多行
【发布时间】:2014-02-04 22:39:45
【问题描述】:

我是安卓新手。最近我正在尝试开发一个可以从 php 获取数据并将其显示到 android listview 中的应用程序。我已经成功地做到了。但问题是将数据显示到列表视图中。我已经成功地每行显示一个项目。但我需要显示两个或更多。我看过很多教程,但我没有意识到这些教程,因为我是 android 新手。我的代码如下,请帮助我现在该怎么办。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new TheTask().execute();
}
   class TheTask extends AsyncTask<Void,Void,String>
  {

@Override
protected String doInBackground(Void... params) {
    String str = null;
    try
    {
      HttpClient httpclient = new DefaultHttpClient();
      HttpPost httppost = new HttpPost("http://10.0.2.2/BSDI/show.php");
      HttpResponse response = httpclient.execute(httppost);
      str =  EntityUtils.toString(response.getEntity());     
    } 
    catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
        }

    return str;

  }

//public void execute() {
    // TODO Auto-generated method stub

//}

@Override
protected void onPostExecute(String result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);

    String response = result.toString();
    try {

        ArrayList<String> stringArray = new ArrayList<String>();
        JSONArray new_array = new JSONArray(response);


        for(int i = 0, count = new_array.length(); i< count; i++)
        {
            try {
                JSONObject jsonObject = new_array.getJSONObject(i);
                stringArray.add(jsonObject.getString("title").toString());


            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        }

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,R.layout.test_tuh,stringArray);           




        ListView list= (ListView) findViewById(R.id.listView1);
        list.setAdapter(adapter);

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        tv.setText("error2");
    } 


} 

这是我的 json 响应

[{"title":"notice1","notice":"details..."},{"title":"exam","notice":"Our exam will be held on 20th january..."}]

【问题讨论】:

  • String result.toString() ???

标签: android listview android-listview


【解决方案1】:

您需要创建一个自定义 ArrayAdapter。关注this link

【讨论】:

    【解决方案2】:
    【解决方案3】:

    行.xml

    <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center_vertical" >
             <TextView
                 android:id="@+id/textView_collage_label_imageno"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentLeft="true"
                 android:layout_alignParentTop="true"
                 android:layout_marginLeft="30dp"
                 android:layout_marginTop="16dp"
                 android:text="No"
                 android:textColor="#000000"
                 android:textSize="20sp" />
             <TextView
                 android:id="@+id/textView_collage_label"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignLeft="@+id/textView_collage_label_imageno"
                 android:layout_below="@+id/textView_collage_label_imageno"
                 android:layout_marginTop="15dp"
                 android:text="Label Name"
                 android:textColor="#000000"
                 android:textSize="18sp" />
        </RelativeLayout>
    

    基础适配器

       package com.example.generaldemo;
    
        import java.util.ArrayList;
    
        import android.content.Context;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.BaseAdapter;
        import android.widget.TextView;
    
        public class CommonBaseAdapter extends BaseAdapter
        {
            ArrayList<String> labelname_array = new ArrayList<String>();
            ArrayList<String> labelno_array = new ArrayList<String>();
    
            Context context;
            LayoutInflater inflater;
    
    
            public CommonBaseAdapter(Context c, ArrayList<String> label_array, ArrayList<String> lno)
            {
                context = c;
                labelname_array = label_array;
                labelno_array = lno;
                //labelno_array = no_array;
                inflater = LayoutInflater.from(context);
            }
    
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return labelname_array.size();
            }
    
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }
    
            @Override
            public long getItemId(int position)
            {
                // TODO Auto-generated method stub
                return position;
            }
            @Override
            public View getView(int position, View convertView, ViewGroup parent)
            {
                View v = convertView;
                Holder holder;
                // TODO Auto-generated method stub
                if (v == null) {
                    v = inflater.inflate(R.layout.row, null);
                    holder = new Holder();
                    holder.tv_labelname = (TextView)v.findViewById(R.id.textView_collage_label);
                    holder.tv_labelno = (TextView) v.findViewById(R.id.textView_collage_label_imageno);
                    v.setTag(holder);
                } else {
                    holder = (Holder) v.getTag();
                }
    
    
                holder.tv_labelname.setText(labelname_array.get(position));
                holder.tv_labelno.setText(labelno_array.get(position));
    
    
                //holder.tv_labelno.setText(labelno_array.get(position));
    
                return v;
            }
            public class Holder
            {
                TextView tv_labelname,tv_labelno;
            }
    
        }
    

    主要活动

    package com.example.generaldemo;
    
    import java.util.ArrayList;
    
    import it.sephiroth.android.wheel.view.Wheel;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.ListView;
    
    public class MainActivity extends Activity {
    
        ListView listview;
    
        ArrayList<String> label_name = new ArrayList<String>();
        ArrayList<String> label_no = new ArrayList<String>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listview = (ListView) findViewById(R.id.listView1);
    
            //add json value in arraylist in your code
            label_name.add("Java");
            label_name.add("PHP");
            label_name.add("Android");
    
            label_no.add("1");
            label_no.add("2");
            label_no.add("3");
    
            CommonBaseAdapter adapter = new CommonBaseAdapter(MainActivity.this, label_name,label_no);
            listview.setAdapter(adapter);
    
        }
    
    }
    

    【讨论】:

    • 感谢您的回复。我已经尝试过你的代码并且它有效。但是在您的代码中,数据是静态的。我的数据是动态的。我首先使用 AsyncTask 从 php 获取 json 数据,然后对其进行解析,然后显示到 listview 中。由于新的 android 开发人员,我在使用您的代码时遇到问题。在获取和解析 json 数据后,我不知道如何使用(显示多行列表视图动态数据)您的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 2011-01-26
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多