如何在listview android中显示多行

时间:2014-02-04 13:31:42

标签: android listview android-listview

我是android新手。最近我正在尝试开发一个可以从php获取数据并将其显示到android listview的应用程序。我成功地做到了。但问题是将数据显示到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..."}]

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

答案 2 :(得分:0)

row.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>

baseAdapter

   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);

    }

}