gpt4 book ai didi

php - ListView 重复值

转载 作者:行者123 更新时间:2023-11-29 12:00:17 26 4
gpt4 key购买 nike

我正在制作一个应用程序来显示 Mysql 数据库中的数据。我使用了 ListView,但是如果我有以下数据,则 ListView 会出现问题:A乙Cd到 z。listView 正确显示了记录数,但是记录是重复的,当我向下然后向上到第一行时,我发现第一个 Item 不是它更改的。哪里有问题 ?我在android中有三个类主要 Activity

获取学生姓名

获取学生姓名适配器

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

最佳答案

看下面的代码

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

关于php - ListView 重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32495185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com