如何使位置自动增加并避免在列表视图中获得重复结果?

时间:2014-12-14 10:24:52

标签: android mysql listview android-arrayadapter

我有一个listview和一个自定义适配器,我得到2个不同的结果,但只有一个被放置两次。如何制作它们以便它们都显示出来?这是我的适配器,我相信问题所在:

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public final class SearchAdapter extends ArrayAdapter<ProblemInfo> {

Context context;
ArrayList<ProblemInfo> problem;
LayoutInflater vi;
  int resource;

  public SearchAdapter(Context context, int resource, ArrayList<ProblemInfo> data) {
      super(context, resource, data);

      vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      problem = data;

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

   final ViewHolder holder;
   if(convertView ==null){
    convertView = vi.inflate(R.layout.searchvehiclerow,parent,false);
    holder = new ViewHolder();

    holder.tvMilage = (TextView)convertView.findViewById(R.id.item_txtMilage);
    holder.tvProblem = (TextView)convertView.findViewById(R.id.item_txtProblem);

    convertView.setTag(holder);

   } else {
    holder = (ViewHolder)convertView.getTag();

   }
   ProblemInfo ProblemInfo = problem.get(position);
   holder.tvProblem.setText(ProblemInfo.getProbdesc());
   holder.tvMilage.setText(ProblemInfo.getMilage());

   return convertView;

 }
 static class ViewHolder {
  public TextView tvMilage;
   public TextView tvProblem;
  }
}

这是我从数据库中拉出来的;

 public class ProblemInfoAsynTask extends AsyncTask<String, Void, Boolean>{
 @Override
  protected Boolean doInBackground(String... urls) {
 // TODO Auto-generated method stub
 String resultprob = "";
 InputStream isrProb = null;
try{

HttpPost httppost = new HttpPost(urls[0]);  
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
int status = response.getStatusLine().getStatusCode();
if(status == 200){
HttpEntity entity = response.getEntity();
isrProb = entity.getContent();

try{
     BufferedReader reader = new BufferedReader(new InputStreamReader(isrProb,"iso-8859-1"),8);
     StringBuilder sb = new StringBuilder();
     String line = null;
     while ((line = reader.readLine()) != null) {
             sb.append(line + "\n");
     }
     isrProb.close();

     resultprob=sb.toString();
}
catch(Exception e){
     Log.e("log_tag", "Error  converting result "+e.toString());
 }
 JSONArray jArray = new JSONArray(resultprob);

for(int i=0; i<jArray.length();i++){
ProblemInfo problem = new ProblemInfo();
JSONObject json = jArray.getJSONObject(i);

problem.setMilage(json.getInt("milage"));
problem.setProbdesc(json.getString("problem_description"));
problem.setId(json.getLong("problem_id"));
probList.add(problem);
   }
return true;
}

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


return false;
}
@Override
protected void onPostExecute(Boolean result) {

super.onPostExecute(result);
if(result == false){


}else {
SearchAdapter probAdapter = new SearchAdapter(getApplicationContext(),android.R.layout.simple_list_item_1,probList);
searchView.setAdapter(probAdapter);
}
}

这是我的数组列表

public class ProblemInfo {

    private static int milage;
    private static String probdesc;
    private long id;
    String milage2;

    public ProblemInfo()
    {

    }

    public void setMilage(int milage) {
        ProblemInfo.milage = milage;
    }

    public String getProbdesc() {
        return probdesc;
    }

    public void setProbdesc(String probdesc) {
        ProblemInfo.probdesc = probdesc;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
    public String getMilage()
    {
        milage2 = Integer.toString(milage);
    return milage2;
    }

}

我有milage2到字符串,以便我可以将它打印到文本框中我认为这是一个问题之前所以我想确保它不会

1 个答案:

答案 0 :(得分:0)

我修改了你的代码,我自己没有测试过,但试一试..

<强> ProblemInfo.java

public class ProblemInfo {

private static int milage;
private static String probdesc;
private long id;
String milage2;

public ProblemInfo(String probdesc, long id,String  milage2)
{

 this.probdesc = probdesc;
 this.id = id;
 this.milage2 = milage2;  
}   

public String getProbdesc() {
    return probdesc;
}
public long getId() {
    return id;
}    
public String getMilage()
{
    milage2 = Integer.toString(milage);
    return milage2;
}

}

ProblemInfoAsynTask 中 取代

problem.setMilage(json.getInt("milage"));
problem.setProbdesc(json.getString("problem_description"));
problem.setId(json.getLong("problem_id"));
probList.add(problem);

probList.add(new ProblemInfo(json.getString("problem_description"), json.getLong("problem_id")),    json.getInt("milage") ); 

并替换

SearchAdapter probAdapter = new    SearchAdapter(getApplicationContext(),android.R.layout.simple_list_item_1,probList);
searchView.setAdapter(probAdapter);

SearchAdapter probAdapter = new SearchAdapter(getApplicationContext());
searchView.setAdapter(probAdapter);
probAdapter.addAll(probList);
probAdapter.notifyDataSetChanged();
SearchAdapter.java

中的

替换

  public SearchAdapter(Context context, int resource, ArrayList<ProblemInfo> data) {
  super(context, resource, data);

  vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  problem = data;

  }

public SearchAdapter(Context context) {
  super(context, R.layout.yourlyaout);

  vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  }

并使用

ProblemInfo ProblemInfo = getItem(position);

而不是

ProblemInfo ProblemInfo = problem.get(position);

<强>更新

ArrayList<ProblemInfo> probList = new ArrayList<ProblemInfo>();

我希望这会有用,如果你遇到任何问题,请告诉我。