android自定义列表视图现在显示

时间:2017-06-19 08:10:38

标签: java android

我有自定义列表视图,我提供了所有必需的代码,但我的自定义列表视图仍未显示。我很想知道你是否可以帮助我,这是代码。

MainActivity.java

    public void parseJSON (String strJSON){
    try {

        person_name = new ArrayList<>();
        id_list = new ArrayList<>();
        personCase = new ArrayList<>();
        caseNumber= new ArrayList<>();
        person_img= new ArrayList<>();
        jsonObject = new JSONObject(strJSON);
        jsonArray = jsonObject.getJSONArray("persons");

        int i =0;
        while(jsonArray.length()>i){
            jsonObject = jsonArray.getJSONObject(i);
            String strid = jsonObject.getString("id");
            String strname = jsonObject.getString("name");
            String strperson_case = jsonObject.getString("person_case");
            String strcaseNumber = jsonObject.getString("caseNumber");
            String strperson_img = jsonObject.getString("person_img");


            personCase.add(strperson_case);
            person_name.add(strname);
            id_list.add(strid);
            caseNumber.add(strcaseNumber);
            person_img.add(strperson_img);
            i++;
        }
        ListView listView = (ListView) findViewById(R.id.listview);
        CustomListView mAdapter =  new CustomListView(MainActivity.this, person_name,personCase,caseNumber,id_list );
        listView.setAdapter(null);
        listView.setAdapter(mAdapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                positionList = position;
                Toast.makeText(MainActivity.this, "" +
                        "ID: " +id_list.get(positionList)+
                        "\nNAME: "+person_name.get(positionList)+
                        "\nCase: "+ personCase.get(positionList)+
                        "\nCase Number: "+ caseNumber.get(positionList), Toast.LENGTH_SHORT).show();


            }
        });
    } catch (Exception e) {
        Log.i("Check",String.valueOf(e));

    }
}

class MyTask extends AsyncTask<Void, Void, String>{
    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(MainActivity.this);
        dialog.setMessage("Fetching some data..");
        dialog.setIndeterminate(false);
        dialog.setCancelable(true);
        dialog.show();
    }

    @Override
    protected String doInBackground(Void... params) {

        try {
            URL url = new URL(MyConfig.URL_STR);
            HttpURLConnection httpURLConnection =
                    (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader =
                    new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb =new StringBuilder();
            String str = "";
            while ((str=bufferedReader.readLine())!=null){
                sb.append(str);
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String strJSON) {
        dialog.dismiss();
        parseJSON(strJSON);
    }
}
}

这是CustomListView.java

package com.example.moy.json;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by Moy on 19/06/2017.
 */

public class CustomListView extends BaseAdapter {
    private Context mContext;
    private ArrayList<String> Name;
    private ArrayList<String> Case;
    private ArrayList<String> CaseNumber;
    private ArrayList<String> Idee;
    LayoutInflater inflater;
    public CustomListView(Context mContext, ArrayList<String> name,
                          ArrayList<String> personCase, ArrayList<String> caseNumber,
                          ArrayList<String> idee) {
        this.mContext = mContext;
        this.Name = name;
        this.Case = personCase;
        this.CaseNumber = caseNumber;
        this.Idee = idee;
         inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View listView;

        if(convertView == null){

            listView = inflater.inflate(R.layout.customlistview, null);

            TextView personCase = (TextView) listView.findViewById(R.id.personCase);
            TextView personName = (TextView) listView.findViewById(R.id.personName);
            TextView personCaseNumber = (TextView) listView.findViewById(R.id.personCaseNum);
            ImageView image = (ImageView) listView.findViewById(R.id.imageView);

            personName.setText(Name.get(position));
            personCase.setText(Case.get(position));
            personCaseNumber.setText(CaseNumber.get(position));
            Picasso.with(mContext)
                    .load("http://192.168.0.11/android/img/"+Idee.get(position)+".png")
                    .into(image);
        }
        else{
            listView = convertView;
        }

        return listView;
    }
}

1 个答案:

答案 0 :(得分:0)

您正在为计数返回0。

@Override
public int getCount() {
    return 1;
}