我有一个列表视图用于显示名称。如果我滚动屏幕,它会显示错误的名称。我知道这是一个常见的错误,根源在于适配器的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;
}
运行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>