2017-06-19 100 views
-2

我有自定义listview,我提供了所有必要的代码,但仍然是我的自定义listview不显示。我很想知道你是否可以帮助我,下面是代码。android自定义listview现在显示

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; 
    } 
} 
+0

为什么需要'listView.setAdapter(null);'? –

+0

你的getCount不应该返回0,但是你的数据的大小 –

回答

0

您为计数返回0。

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

它在我将它改为1后返回一个错误,它表示illefalAtgumentException:目标不能为空 –

+0

而你通过返回0来修复它? :D调查知道为什么会出现该错误。 –

+0

谢谢!有用!我从毕加索那里得到了另一个。谢谢! –