ListView重复值

时间:2015-09-10 06:58:26

标签: php android mysql listview

我正在创建一个应用程序来显示来自Mysql数据库的数据。我使用了ListView,但如果我的数据如下,我的列表视图有问题: 一个 b C d 到z。 listView正确地显示了记录的数量,但记录被重复,当我下降时,我走到第一行,我发现第一个项目不是它的变化。问题出在哪儿 ? 我在android中有三个类 MainActivity

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

1 个答案:

答案 0 :(得分:1)

看看下面的代码

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