嗨朋友我是android的新手。我有一个自定义列表视图,每个项目包含单个文本和图像视图首先我使用单个图像为所有listitem然后我使用一个数组列表上点击我添加一个元素后,我通知数据集已更改但事情是这只是更新列表视图的最后一项是我的代码
public class MainActivity extends Activity {
String[] countries = { "pakistan", "china", "russia", "indonesia", "japan",
"bangladesh", "Brazil", "india", "nigeria",
"united states of america" };
String[] population = { "180 million", "1500 million", "160 million",
"200 million", "140 million", "160 million", "200 million",
"1200 million", "120 million", "280 million" };
int[] images = { R.drawable.pakistan, R.drawable.china, R.drawable.russia,
R.drawable.ic_launcher, R.drawable.japan, R.drawable.bangladesh,
R.drawable.brazil, R.drawable.india, R.drawable.nigeria,
R.drawable.unitedstates };
ArrayList<String> compare=new ArrayList<String>();
int mode = 1;
Context context;
Animation anim,anim1;
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = MainActivity.this;
final ListView lv = (ListView) findViewById(R.id.mylist);
lv.setAdapter(new adopter(this));
Button stopAnim=(Button)findViewById(R.id.stopanimation);
stopAnim.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(i<countries.length){
compare.add(countries[i]);
i++;
}
BaseAdapter nain=new adopter(MainActivity.this);
nain.notifyDataSetChanged();
lv.setAdapter(nain);
}
});
Button click = (Button) findViewById(R.id.Animationbutt);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
class adopter extends BaseAdapter {
final LayoutInflater layoutinflator;
public adopter(MainActivity mainActivity) {
layoutinflator = getLayoutInflater();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return countries.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listitem = convertView;
int pos = position;
// listitem = layoutinflator.inflate(R.layout.anotherxml, null);
listitem = layoutinflator.inflate(R.layout.another1, null);
// TextView tv = (TextView) listitem.findViewById(R.id.tv2);
// TextView tv1 = (TextView) listitem.findViewById(R.id.tv1);
TextView tv = (TextView) listitem.findViewById(R.id.Songtitle);
tv.setText(countries[pos]);
ImageView iv = (ImageView) listitem.findViewById(R.id.iv);
Log.d("Compare size is this","#"+compare.size());
if(compare.size()!=0){
for(int j=0;j<compare.size();j++){
if(countries[pos].equalsIgnoreCase(compare.get(j).toString())){
Log.d("Countries compare are this", "hasnain"+countries[pos]);
iv.setBackgroundResource(images[pos]);
}else{
iv.setBackgroundResource(R.drawable.pakistan);
}
}
}else{
iv.setBackgroundResource(R.drawable.pakistan);
}
return listitem;
}
}
}
我无法理解这种行为,请帮助
答案 0 :(得分:0)
您滥用notifyDataSetChanged()
构造。我们的想法是保持相同的适配器并修改你喂它的列表。
因此,将适配器存储为实例变量,然后修改它使用的列表(国家/地区)并调用notifyDataSetChanged()
。为此,您应使用ArrayList<String>
(或其他List实现)而不是String[]
,因为String[]
的长度无法修改。
答案 1 :(得分:0)
每次单击按钮时都会创建适配器。不要那样做。创建一个适配器实例并在onCreate中初始化它。在按钮单击中,只需通知适配器。
代码就在这里......
adopter my_adapter ;
@Override protected void onCreate(BundlesavedInstanceState) {
...
...
my_adapter = newadopter(MainActivity.this);
lv.setAdapter(my_adapter);
...
...
Button stopAnim=(Button)findViewById(R.id.stopanimation);
stopAnim.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(i<countries.length){
compare.add(countries[i]);
i++;
}
my_adapter.notifyDataSetChanged();
}
});
}