ListView 在 Scroll 上显示错误的项目
ListView shows wrong items on Scroll
我有一个用于显示姓名的列表视图。如果我滚动屏幕,它会显示错误的名称。我知道这是一个根植于适配器的 getView 方法的常见错误,但是尽管遵循了许多现有讨论中提出的方法,但仍无法解决它。
我已经在下面粘贴了我的代码。如果有人可以识别错误,那就太好了。
自定义适配器
public class ListArrayAdapter extends ArrayAdapter<client> {
HashMap<String, Integer> hMap = new HashMap<String, Integer>();
private Context cntxt;
private List<client> client_list;
private int textviewres;
public ListArrayAdapter(Context context, int textViewResourceId, List<client> objects) {
super(context,textViewResourceId,objects);
this.cntxt = context;
this.textviewres = textViewResourceId;
this.client_list = objects;
for(int j=0; j<client_list.size(); j++){
Log.d("ListArrayAdapter", "Position No" +j +": " +client_list.get(j).getName());
Log.d("ListArrayAdapter", "Total elements in the client list is "+client_list.size());
}
}
@Override
public long getItemId(int position) {
client item = getItem(position);
return item.getID();
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View clist;
LayoutInflater inflater = (LayoutInflater) cntxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
clist = inflater.inflate(textviewres, null);
TextView tv = (TextView) clist.findViewById(R.id.textv);
tv.setText(client_list.get(position).getName());
} else {
clist = convertView;
}
return clist;
}
}
Activity 运行 ListView
public class clients_list extends ActionBarActivity {
private database_handler obj;
private Context context;
static ArrayAdapter<String> adapter;
List<client> values;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clients);
list = (ListView) findViewById(R.id.clientlist);
context = getBaseContext();
obj = new database_handler(context);
final List<String> listdata = new ArrayList<String>();
values = obj.getAllClients();
int listlength = values.size();
for (int i = 0; i < listlength; ++i) {
listdata.add(values.get(i).getName());
}
ListArrayAdapter adapter = new ListArrayAdapter(this,R.layout.custom_textv, values);
list.setAdapter(adapter);
//Define what happens after clicking the Add Client Button
Button add_client_button = (Button) findViewById(R.id.addbutton);
add_client_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent addclient = new Intent(v.getContext(), add_client.class);
v.getContext().startActivity(addclient);
}
});
list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Display Client Details
Intent displayclient = new Intent(view.getContext(), display_clientdetails.class);
Long db_id = parent.getAdapter().getItemId(position);
String TAG = "client_list";
Log.d(TAG,"Position is" +position +" ID is " +db_id);
displayclient.putExtra("client_id",db_id);
view.getContext().startActivity(displayclient);
}
});
}
@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_clients, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Activity
的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@color/green"
tools:context="com.example.jaya.myapplication.Clients">
<Button
android:id="@+id/addbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_addnewclient"
android:text="@string/add_client"
android:textColor="@color/white"/>
<ListView
android:id="@+id/clientlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/addbutton"
android:dividerHeight="5dp">
</ListView>
我觉得你这里有问题
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View clist;
LayoutInflater inflater = (LayoutInflater) cntxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
clist = inflater.inflate(textviewres, null);
TextView tv = (TextView) clist.findViewById(R.id.textv);
tv.setText(client_list.get(position).getName());
} else {
clist = convertView;
}
return clist;
}
因此,当您的转换视图不为空时,您不会像这样填充列表
TextView tv = (TextView) clist.findViewById(R.id.textv);
tv.setText(client_list.get(position).getName());
我一般都是这样做的
public class MyHolder {
TextView keyword, tweetCount;
public MyHolder(View v) {
keyword = (TextView) v.findViewById(R.id.keyword_text);
tweetCount = (TextView) v.findViewById(R.id.tweets_count_text);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
MyHolder holder;
if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.keyword_contents, parent, false);
holder = new MyHolder(view);
view.setTag(holder);
} else {
holder = (MyHolder) view.getTag();
}
holder.keyword.setText(list.get(position).getKeyword());
holder.tweetCount.setText(String.valueOf(list.get(position).getCount()));
return view;
}
我认为 holder 会做的不是一次又一次地查找 textview 它只会 findviewbyid 一次
我可以在阅读 ListView in ArrayAdapter order get's mixed up when scrolling 后自行解决问题。
错误出在getView方法上。我已经将用于将名称分配给 ListView 中的 TextView 的代码放置在 If Block for (convertView == null) 中。由于在显示第一组项目后 convertView 不为 null,因此代码无法为 Textviews 分配新名称 wand 只是使用 Android 放置在 convertView 中的可重用项目。
正确的getView代码如下:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View clist = convertView;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) cntxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
clist = inflater.inflate(textviewres, null);
}
TextView tv = (TextView) clist.findViewById(R.id.textv);
tv.setText(client_list.get(position).getName());
return clist;
}
鼓励使用 ViewHolder
模式在 getView(...)
中制作 View
。否则只需更改您的代码即可:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View clist;
LayoutInflater inflater = (LayoutInflater) cntxt.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
clist = inflater.inflate(textviewres, null);
TextView tv = (TextView) clist.findViewById(R.id.textv);
tv.setText(client_list.get(position).getName());
return clist;
}
希望对您有所帮助!
我有一个用于显示姓名的列表视图。如果我滚动屏幕,它会显示错误的名称。我知道这是一个根植于适配器的 getView 方法的常见错误,但是尽管遵循了许多现有讨论中提出的方法,但仍无法解决它。
我已经在下面粘贴了我的代码。如果有人可以识别错误,那就太好了。
自定义适配器
public class ListArrayAdapter extends ArrayAdapter<client> {
HashMap<String, Integer> hMap = new HashMap<String, Integer>();
private Context cntxt;
private List<client> client_list;
private int textviewres;
public ListArrayAdapter(Context context, int textViewResourceId, List<client> objects) {
super(context,textViewResourceId,objects);
this.cntxt = context;
this.textviewres = textViewResourceId;
this.client_list = objects;
for(int j=0; j<client_list.size(); j++){
Log.d("ListArrayAdapter", "Position No" +j +": " +client_list.get(j).getName());
Log.d("ListArrayAdapter", "Total elements in the client list is "+client_list.size());
}
}
@Override
public long getItemId(int position) {
client item = getItem(position);
return item.getID();
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View clist;
LayoutInflater inflater = (LayoutInflater) cntxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
clist = inflater.inflate(textviewres, null);
TextView tv = (TextView) clist.findViewById(R.id.textv);
tv.setText(client_list.get(position).getName());
} else {
clist = convertView;
}
return clist;
}
}
Activity 运行 ListView
public class clients_list extends ActionBarActivity {
private database_handler obj;
private Context context;
static ArrayAdapter<String> adapter;
List<client> values;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clients);
list = (ListView) findViewById(R.id.clientlist);
context = getBaseContext();
obj = new database_handler(context);
final List<String> listdata = new ArrayList<String>();
values = obj.getAllClients();
int listlength = values.size();
for (int i = 0; i < listlength; ++i) {
listdata.add(values.get(i).getName());
}
ListArrayAdapter adapter = new ListArrayAdapter(this,R.layout.custom_textv, values);
list.setAdapter(adapter);
//Define what happens after clicking the Add Client Button
Button add_client_button = (Button) findViewById(R.id.addbutton);
add_client_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent addclient = new Intent(v.getContext(), add_client.class);
v.getContext().startActivity(addclient);
}
});
list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Display Client Details
Intent displayclient = new Intent(view.getContext(), display_clientdetails.class);
Long db_id = parent.getAdapter().getItemId(position);
String TAG = "client_list";
Log.d(TAG,"Position is" +position +" ID is " +db_id);
displayclient.putExtra("client_id",db_id);
view.getContext().startActivity(displayclient);
}
});
}
@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_clients, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Activity
的布局<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@color/green"
tools:context="com.example.jaya.myapplication.Clients">
<Button
android:id="@+id/addbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_addnewclient"
android:text="@string/add_client"
android:textColor="@color/white"/>
<ListView
android:id="@+id/clientlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/addbutton"
android:dividerHeight="5dp">
</ListView>
我觉得你这里有问题
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View clist;
LayoutInflater inflater = (LayoutInflater) cntxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
clist = inflater.inflate(textviewres, null);
TextView tv = (TextView) clist.findViewById(R.id.textv);
tv.setText(client_list.get(position).getName());
} else {
clist = convertView;
}
return clist;
}
因此,当您的转换视图不为空时,您不会像这样填充列表
TextView tv = (TextView) clist.findViewById(R.id.textv);
tv.setText(client_list.get(position).getName());
我一般都是这样做的
public class MyHolder {
TextView keyword, tweetCount;
public MyHolder(View v) {
keyword = (TextView) v.findViewById(R.id.keyword_text);
tweetCount = (TextView) v.findViewById(R.id.tweets_count_text);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
MyHolder holder;
if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.keyword_contents, parent, false);
holder = new MyHolder(view);
view.setTag(holder);
} else {
holder = (MyHolder) view.getTag();
}
holder.keyword.setText(list.get(position).getKeyword());
holder.tweetCount.setText(String.valueOf(list.get(position).getCount()));
return view;
}
我认为 holder 会做的不是一次又一次地查找 textview 它只会 findviewbyid 一次
我可以在阅读 ListView in ArrayAdapter order get's mixed up when scrolling 后自行解决问题。
错误出在getView方法上。我已经将用于将名称分配给 ListView 中的 TextView 的代码放置在 If Block for (convertView == null) 中。由于在显示第一组项目后 convertView 不为 null,因此代码无法为 Textviews 分配新名称 wand 只是使用 Android 放置在 convertView 中的可重用项目。
正确的getView代码如下:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View clist = convertView;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) cntxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
clist = inflater.inflate(textviewres, null);
}
TextView tv = (TextView) clist.findViewById(R.id.textv);
tv.setText(client_list.get(position).getName());
return clist;
}
鼓励使用 ViewHolder
模式在 getView(...)
中制作 View
。否则只需更改您的代码即可:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View clist;
LayoutInflater inflater = (LayoutInflater) cntxt.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
clist = inflater.inflate(textviewres, null);
TextView tv = (TextView) clist.findViewById(R.id.textv);
tv.setText(client_list.get(position).getName());
return clist;
}
希望对您有所帮助!