您好,在我的项目中,我正在使用自定义适配器。当包含列表视图的活动打开时,图像视图和文本视图首先出现,并在一段时间后视图消失。我无法找到解决方案你有什么想法吗? 编辑: 它也可以在模拟器上运行但不能在普通设备上运行吗?
我设置适配器的类:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person_deatil);
Intent detailIntent = getIntent();
Gson gson = new Gson();
String obj = detailIntent.getStringExtra("obj");
PeopleListItem people = gson.fromJson(obj, PeopleListItem.class);
ListView lw = (ListView) findViewById(R.id.perdetLw);
PeopleListItem[] list = new PeopleListItem[6];
for(int i=0; i<6; i++){
list[i] = people;
}
ActionBar ab = getActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setTitle(people.getName());
ab.setIcon(R.drawable.ic_action_person);
PeopleDetailArrayAdapter adp = new PeopleDetailArrayAdapter(this, R.layout.person_detail_item, list);
lw.setAdapter(adp);
}
自定义适配器类:
public class PeopleDetailArrayAdapter extends ArrayAdapter<PeopleListItem> {
int resource;
Context context;
int count = 0;
public PeopleDetailArrayAdapter(Context context, int resource,
PeopleListItem[] list) {
super(context, resource, list);
this.resource = resource;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final PeopleListItem peopleObj = getItem(position);
LayoutInflater layoutInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View listViewItem = layoutInflator.inflate(resource, null);
final TextView textView = (TextView) listViewItem.findViewById(R.id.detailTw);
final TextView textView1 = (TextView) listViewItem.findViewById(R.id.detailTw1);
final ImageView imageView = (ImageView) listViewItem.findViewById(R.id.detailIw);//img view kişinin resmine göre değişmeli
final ImageView imageView1 = (ImageView) listViewItem.findViewById(R.id.detailIw1);
switch (count) {
case 0:
textView.setText("Title:");
String title = peopleObj.getTitle();
textView1.setText(title);
break;
case 1:
textView.setText("Department:");
String dep = peopleObj.getDepart();
textView1.setText(dep);
break;
case 2:
textView.setText("Work Phone:");
String wp = peopleObj.getWorkPhone();
textView1.setText(wp);
imageView.setVisibility(ImageView.VISIBLE);
break;
case 3:
textView.setText("Mobile Phone:");
String mp = peopleObj.getMobilePhone();
textView1.setText(mp);
imageView.setVisibility(ImageView.VISIBLE);
imageView1.setVisibility(ImageView.VISIBLE);
break;
case 4:
textView.setText("Email:");
String email = peopleObj.getEmail();
textView1.setText(email);
break;
case 5:
textView.setText("Manager:");
String man = peopleObj.getManager();
textView1.setText(man);
break;
default:
break;
}
count++;
return listViewItem;
}
}
答案 0 :(得分:0)
我使用position
而非使用count
public class PeopleDetailArrayAdapter extends ArrayAdapter<PeopleListItem> {
int resource;
Context context;
public PeopleDetailArrayAdapter(Context context, int resource,
PeopleListItem[] list) {
super(context, resource, list);
this.resource = resource;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final PeopleListItem peopleObj = getItem(position);
LayoutInflater layoutInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View listViewItem = layoutInflator.inflate(resource, null);
final TextView textView = (TextView) listViewItem.findViewById(R.id.detailTw);
final TextView textView1 = (TextView) listViewItem.findViewById(R.id.detailTw1);
switch (position) {
case 0:
textView.setText("Title");
String title = peopleObj.getTitle();
textView1.setText(title);
break;
case 1:
textView.setText("Department");
String dep = peopleObj.getDepart();
textView1.setText(dep);
break;
case 2:
textView.setText("Work");
String wp = peopleObj.getWorkPhone();
textView1.setText(wp);
break;
case 3:
textView.setText("Mobile");
String mp = peopleObj.getMobilePhone();
textView1.setText(mp);
break;
case 4:
textView.setText("Email");
String email = peopleObj.getEmail();
textView1.setText(email);
break;
case 5:
textView.setText("Manager");
String man = peopleObj.getManager();
textView1.setText(man);
break;
}
return listViewItem;
}
}