我正在尝试从布局中拍摄图像。当用户选择任何图像时,它将显示在自定义列表视图中。我第一次点击图像时所取得的成就显示在自定义列表视图中,但是当我第二次选择图像时,它会覆盖第一个图像。这是审查的代码
这是我的主要课程
package com.example.zeus_technologies.customlistview;
import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView list;
String[] itemname ={
"Safari",
};
Integer[] imgid={
R.drawable.a,
R.drawable.ab,
R.drawable.abc,
R.drawable.abcd,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// list=(ListView)findViewById(R.id.list);
final Context context=this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
ImageView[] im = new ImageView[4];
im[0] = (ImageView) findViewById(R.id.imageview1);
im[1] = (ImageView) findViewById(R.id.imageview2);
im[2] = (ImageView) findViewById(R.id.imageview3);
im[3] = (ImageView) findViewById(R.id.imageview4);
im[0].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CustomListAdapter adapter=new CustomListAdapter((Activity) context,itemname, imgid[0]);
list.setAdapter(adapter);
}
});
im[1].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CustomListAdapter adapter = new CustomListAdapter((Activity) context, itemname, imgid[1]);
list.setAdapter(adapter);
}
});
}
这是我的列表适配器类 package com.example.zeus_technologies.customlistview;
import android.app.Activity;
import android.content.Context;
import android.media.Image;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] itemname;
private final Integer imgid;
public CustomListAdapter(Activity context,String[] itemname ,Integer imgid) {
super(context, R.layout.mylist, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.itemname=itemname;
this.imgid=imgid;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.mylist, null,true);
//TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
//TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
//txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid);
///extratxt.setText("Description "+itemname[position]);
return rowView;
};
}
还有一件事。我已经尝试从onClickListener中创建适配器对象。但仍然无法正常工作答案 0 :(得分:2)
每次向其添加新项目时,您都不想设置新的适配器,您要做的是将适配器设置为listview一次,并更新基础数据集并调用{{1} }。像这样的东西(伪代码):
MainActivity
direction: rtl
适配器:
adapter.notifyDataSetChanged();
列表项:
public class MainActivity extends Activity implements View.OnClickListener {
private List<ListItem> dataSet;
private ListView list;
private CustomListAdapter adapter;
private ImageView imageview1, imageview2, imageview3, imageview4;
private Integer[] ids = {
R.drawable.first,
R.drawable.second,
R.drawable.third,
R.drawable.fourth,
};
private String[] names = {
"First",
"Second",
"Third",
"Fourth",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
dataSet = new ArrayList<>();
adapter = new CustomListAdapter(this, dataSet);
list.setAdapter(adapter);
imageview1 = (ImageView) findViewById(R.id.imageview1);
imageview2 = (ImageView) findViewById(R.id.imageview2);
imageview3 = (ImageView) findViewById(R.id.imageview3);
imageview4 = (ImageView) findViewById(R.id.imageview4);
imageview1.setOnClickListener(this);
imageview2.setOnClickListener(this);
imageview3.setOnClickListener(this);
imageview4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int selectedImage;
if (v == imageview1) {
selectedImage = 0;
} else if (v == imageview2) {
selectedImage = 1;
} else if (v == imageview3) {
selectedImage = 2;
} else {
selectedImage = 3;
}
dataSet.add(new ListItem(names[selectedImage], ids[selectedImage]));
adapter.notifyDataSetChanged();
}
}