0
我正在製作一個應用程序來顯示來自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;
}
}
請添加您的代碼 – Pavya
顯示您getView方法 –
使用viewholder.It是列表視圖 – kgandroid