ListView重复值

ListView重复值

问题描述:

我正在制作一个应用程序来显示来自Mysql数据库的数据。我使用ListView,但我有一个列表视图的问题,如果我有以下数据: a b c d to z。 listView显示正确的记录数,但记录重复,当我走下来,然后我走到第一行,我发现第一个项目不是它改变。哪里有问题 ? 我有三个类在Android MainActivityListView重复值

GetStudentNames

GetStudntNamesAdapter

public class TakingAttendance extends ActionBarActivity { 

private Intent intent; 

private JSONParser jsonParser = new JSONParser(); 


private ListView mListView; 
private ListViewStudentNameAdapter listViewStudentNameAdapter; 
private ArrayList<ListViewStudentNames> listViewStudentNames; 

private String getnames = 
     "http://amjad-test.site40.net/getstudentname.php"; 

private String takingabsence = 
     "http://amjad-test.site40.net/takingattendance.php"; 

private int saving_loop ; 





@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_taking_attendance); 
    mListView = (ListView) findViewById(R.id.Student_names_List); 




    intent = getIntent(); 

    new GetNames().execute(); 


} 

private class GetNames extends AsyncTask<Void, Void, Boolean> 
{ 
    private ProgressDialog mProgressDialog; 


    private JSONObject jsonObjectResult = null; 

    private String error; 


    private GetNames() 
    { 

    } 


    @Override 
    protected void onPreExecute() 
    { 
     super.onPreExecute(); 
     listViewStudentNames = new ArrayList<ListViewStudentNames>(); 
     mProgressDialog = ProgressDialog.show(TakingAttendance.this, 
       "Processing...", "Get last news", false, false); 

    } 

    @Override 
    protected Boolean doInBackground(Void... params) 
    { 
     List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 

     jsonObjectResult = jsonParser.makeHttpRequest(getnames, pairs); 

     if (jsonObjectResult == null) 
     { 
      error = "Error in the connection"; 
      return false; 
     } 

     try 
     { 
      if (jsonObjectResult.getInt("success") == 1) 
      { 
       JSONArray jsonArray = jsonObjectResult.getJSONArray("posts"); 
       saving_loop = jsonArray.length() ; 
       JSONObject news ; 

       for (int i = 0; i < jsonArray.length(); i++) 
       { 
        news = jsonArray.getJSONObject(i); 
        ListViewStudentNames listviewstudentname = new ListViewStudentNames 
          (
            news.getString("STUDENT_NAME"), 
            news.getString("STUDENT_ID") 
          ); 

        listViewStudentNames.add(listviewstudentname); 
        listviewstudentname.setCheckBox(false); 

       } 
       return true; 
      } 
      else 
       error = jsonObjectResult.getString("message"); 

     } 
     catch (Exception ex) 
     { 

     } 

     return false; 
    } 

    @Override 
    protected void onPostExecute(Boolean aBoolean) 
    { 
     super.onPostExecute(aBoolean); 
     mProgressDialog.dismiss(); 
     if (aBoolean) 
     { 
      listViewStudentNameAdapter = new ListViewStudentNameAdapter(TakingAttendance.this, 
        listViewStudentNames); 
      mListView.setAdapter(listViewStudentNameAdapter); 
     } 
     else 
      Toast.makeText(TakingAttendance.this, error, Toast.LENGTH_LONG).show(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_taking_attendance, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

第二类:

public class ListViewStudentNames { 
private String StudentName ; 
private String StudentID ; 
private boolean selected = false ; 

public ListViewStudentNames(String StudentName, String StudentID) 
{ 
    this.StudentName = StudentName ; 
    this.StudentID = StudentID; 

} 
public ListViewStudentNames(String StudentID) 
{ 
    this.StudentID = StudentID; 

} 
public boolean getCheckBox() { 
    return selected; 
} 

public void setCheckBox(boolean selected) { 
    this.selected = selected; 
} 

public String getStudentID() { 
    return StudentID; 
} 

public void setStudentID(String studentID) { 
    StudentID = studentID; 
} 

public String getStudentName() { 
    return StudentName; 
} 


public void setStudentName(String studentName) { 
    StudentName = studentName; 
} 

}

第三类:

public class ListViewStudentNameAdapter extends ArrayAdapter<ListViewStudentNames> { 

    private Context mContext; 
    private ArrayList<ListViewStudentNames> mData; 

    private class ViewHolder { 
     CheckBox name; 
    } 

    public ListViewStudentNameAdapter (Context mContext, ArrayList<ListViewStudentNames> mData) { 
     super(mContext, R.layout.student_names_shape, mData); 
     this.mContext = mContext; 
     this.mData = new ArrayList<ListViewStudentNames>(); 
     this.mData.addAll(mData); 
    } 

    @Override 
    public int getCount() { 
     return mData.size(); 
    } 

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

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) 
    { 
     ViewHolder holder = null; 
     Log.v("ConvertView", String.valueOf(position)); 



     if (convertView == null) 
     { 

      LayoutInflater mInflater = (LayoutInflater) 
        mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
      convertView = mInflater.inflate(R.layout.student_names_shape, null); 
      holder = new ViewHolder(); 
      holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1); 
      convertView.setTag(holder); 

      TextView Student_name = (TextView) convertView.findViewById(R.id.Student_name); 
      Student_name.setText(mData.get(position).getStudentName()); 
      holder.name.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        CheckBox cb = (CheckBox) v; 
        boolean x; 

        ListViewStudentNames country = (ListViewStudentNames) cb.getTag(); 


        if (cb.isChecked()) { 
         country.setCheckBox(true); 
        } else { 
         country.setCheckBox(false); 
        } 

       } 
      }); 

     } 

     else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     ListViewStudentNames country = mData.get(position); 

     holder.name.setChecked(country.getCheckBox()); 
     holder.name.setTag(country); 

     return convertView; 
    } 
} 
+0

请添加您的代码 – Pavya

+0

显示您getView方法 –

+0

使用viewholder.It是列表视图 – kgandroid

看看下面的代码

private class ViewHolder { 
     CheckBox name; 
     TextView Student_name; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) 
{ 
    ViewHolder holder = null; 
    Log.v("ConvertView", String.valueOf(position)); 
     String student_Name=mData.get(position).getStudentName(); 


    if (convertView == null) 
    { 

     LayoutInflater mInflater = (LayoutInflater) 
       mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     convertView = mInflater.inflate(R.layout.student_names_shape, null); 
     holder = new ViewHolder(); 
     holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1); 
     convertView.setTag(holder); 

     holder.Student_name = (TextView)  convertView.findViewById(R.id.Student_name); 


    } 

    else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 
    ListViewStudentNames country = mData.get(position); 

    holder.name.setChecked(country.getCheckBox()); 
    holder.name.setTag(country); 




    holder.Student_name .setText(""+student_Name); 
     holder.name.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       CheckBox cb = (CheckBox) v; 
       boolean x; 

       ListViewStudentNames country = (ListViewStudentNames) cb.getTag(); 


       if (cb.isChecked()) { 
        country.setCheckBox(true); 
       } else { 
        country.setCheckBox(false); 
       } 

      } 
     }); 

    return convertView; 
} 
+0

我会试试看。等一下 – ama989