自定义ListView只显示一个项目

时间:2015-11-09 11:52:09

标签: android listview

我有一个显示一些数据的自定义列表视图。我从服务器以JSON格式获取此数据。这就是我获取数据的方式

     private class NetCheck extends AsyncTask<Void, Void, Void> {

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

        samplePaperHomeAdapter = new SamplePaperHomeAdapter(SamplePaperActivity.this,
                sampleTests);
        mListView1.setAdapter(samplePaperHomeAdapter);

    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        try {

            HttpClient httpClient = new DefaultHttpClient();

            HttpPost httpRequest = new HttpPost(

                   "http://url");

            httpRequest.setHeader("Content-Type", "application/json");

            JSONObject json = new JSONObject();

            SharedPreferences preff = getSharedPreferences(
                    "MyPref", MODE_PRIVATE);
            String stud_id = preff.getString("id", null);

            json.put("userid", stud_id);

            Log.e("JSON Object", json.toString());

            StringEntity se = new StringEntity(json.toString());

            se.setContentEncoding("UTF-8");
            se.setContentType("application/json");

            httpRequest.setEntity(se);
            HttpResponse httpRes = httpClient.execute(httpRequest);

            java.io.InputStream inputStream = httpRes.getEntity()
                    .getContent();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream);
            BufferedReader reader = new BufferedReader(inputStreamReader);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            inputStream.close();
            strServerResponse = sb.toString();


            if (strServerResponse != null) {
                try {

                    JSONObject jsonObj = new JSONObject(strServerResponse);

                    JSONArray mainDataObject = jsonObj
                            .getJSONArray("attemptedtest");

                    for (int i = 0; i < mainDataObject.length(); i++) {

                        pojo = new Pojo();
                        JSONObject jobj2 = mainDataObject
                                .getJSONObject(i);

                    }

                    JSONObject jsonObj11 = new JSONObject(strServerResponse);
                    // String DataStatus = jsonObj.getString("status");
                    pojo = new Pojo();

                    JSONArray mainDataObject11 = jsonObj11
                            .getJSONArray("moretest");
                    for (int i = 0; i < mainDataObject11.length(); i++) {


                        JSONObject jobj211 = mainDataObject11
                                .getJSONObject(i);

                        String srno  = jobj211.optString("srno");
                        String name = jobj211.optString("name");
                        String total_question = jobj211.optString("total_question");
                        String total_time = jobj211.optString("total_time");
                        String paper_id = jobj211.optString("paper_id");

                        pojo.setSampletestId(paper_id);
                        pojo.setSampleTestTime(total_time);
                        pojo.setSampleTestName(name);
                        pojo.setSampleTestTotalQues(total_question);

                        sampleTests.add(pojo);

                    }

这是SamplePaperHomeAdapter类

 public class SamplePaperHomeAdapter extends BaseAdapter {
private Context activity;
ArrayList<Pojo> data;
private ArrayList<Pojo> arraylist = null;
public static LayoutInflater inflater;
TextView sample_name, sample_ques, sample_time ;
Pojo pojo;

public SamplePaperHomeAdapter(Context a, ArrayList<Pojo> al) {
    // TODO Auto-generated constructor stub
    activity = a;
    data = al;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.arraylist = new ArrayList<Pojo>();
    this.arraylist.addAll(data);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return data.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View v = convertView;

    v = inflater.inflate(R.layout.sampletest_home_item, parent, false);
    pojo = data.get(position);
    sample_name = (TextView) v.findViewById(R.id.sampletest_name);
    sample_name.setText(pojo.getSampleTestName());

    sample_ques = (TextView) v.findViewById(R.id.sampletest_ques);
    sample_ques.setText(pojo.getSampleTestTotalQues());

    sample_time = (TextView) v.findViewById(R.id.sampleTestTime);
    sample_time.setText(pojo.getSampleTestTime());


    return v;

}

这是activity_sample_paper.xml

  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="Appeared Tests" />

    <ListView
        android:id="@+id/sampleTestListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"></ListView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:gravity="center_vertical"
        android:text="New Tests" />

    <ListView
        android:id="@+id/appearedTestListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"></ListView>
</LinearLayout>

JSON

  {"attemptedtest":[],"moretest":[{"srno":1,"name":"Mock Test 3","total_question":"200","total_time":"120","paper_id":"3"},{"srno":2,"name":"Mock test 2","total_question":"51","total_time":"30","paper_id":"2"},{"srno":3,"name":"MOCK Test 5","total_question":"15","total_time":"12","paper_id":"8"}]}

这里我只在ListView中获取一个项目。我从json和setter函数获取正确的数据也可以正常工作。来自getter函数我只得到最后一个值..请帮助我..

1 个答案:

答案 0 :(得分:0)

在适配器类中尝试以下getview,

@Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub
        View myView = convertView;
        final ViewHolder holder;
        try {

            if (myView == null) {
                LayoutInflater inflater = LayoutInflater.from(context);
                myView = inflater.inflate(R.layout.row_list, null);
                holder = new ViewHolder();

                holder.txtHeader= (TextView) myView.findViewById(R.id.txtHeader);

                myView.setTag(holder);
            } else {
                holder = (ViewHolder) myView.getTag();
            }
            holder.txtHeader.setText(""+ alist.get(position).getStrTitle());

            return myView;
        } catch (Exception e) {
            return myView;
        }
    }