Android Listview单击事件不工作
我正在列表视图中显示图像和文件名。下面的代码填充了图像和文件名,但我无法单击listview中的项目。单击listview中的项目时,活动就会崩溃。请帮我弄清楚。谢谢Android Listview单击事件不工作,android,Android,我正在列表视图中显示图像和文件名。下面的代码填充了图像和文件名,但我无法单击listview中的项目。单击listview中的项目时,活动就会崩溃。请帮我弄清楚。谢谢 public class ListviewActivity extends Activity { public class Data_Adapter extends ArrayAdapter<Data_Class> { Context context; int ResourceLayoutId;
public class ListviewActivity extends Activity
{
public class Data_Adapter extends ArrayAdapter<Data_Class>
{
Context context;
int ResourceLayoutId;
ArrayList<Data_Class> data = null;
public Data_Adapter(Context c, int r, ArrayList<Data_Class> dc)
{
super(c, r, dc);
this.ResourceLayoutId = r;
this.context = c;
this.data = dc;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
DataHolder holder = null;
if (row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(ResourceLayoutId, parent, false);
holder = new DataHolder();
holder.image = (ImageView) row.findViewById(R.id.image1);
holder.txt = (TextView) row.findViewById(R.id.textlist);
row.setTag(holder);
} else
{
holder = (DataHolder) row.getTag();
}
Data_Class dc = data.get(position);
holder.txt.setText(dc.data);
Bitmap bm = decodeSampledBitmapFromUri(data.get(position).get_path(), 100, 100);
holder.image.setImageBitmap(bm);
return row;
}
public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight)
{
Bitmap bm = null;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight)
{
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth)
{
if (width > height)
{
inSampleSize = Math.round((float) height / (float) reqHeight);
} else
{
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}
public class DataHolder
{
ImageView image;
TextView txt;
}
}
private ListView listview1;
private int check_view;
private File targetDirectory;
private File[] files;
protected static ArrayList<Data_Class> dataclass = new ArrayList<Data_Class>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
check_view = 0;
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_layout);
listview1 = (ListView) findViewById(R.id.List1);
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
String targetPath = path + "/MyApp/";
targetDirectory = new File(targetPath);
files = targetDirectory.listFiles();
for (int i = 0; i < files.length; i++)
{
dataclass.add(new Data_Class(files[i].getName(), files[i].getAbsolutePath()));
}
Data_Adapter adapter = new Data_Adapter(this, R.layout.img, dataclass);
listview1.setAdapter(adapter);
listview1.setClickable(true);
// activity crashes when trying clicking item in listview
listview1.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
// TODO Auto-generated method stub
String path = (String) parent.getItemAtPosition(position);
Toast.makeText(ListviewActivity.this, path, Toast.LENGTH_LONG).show();
if(path.contains(".pdf")){
String path2 = path.substring(path.lastIndexOf("/") + 1);
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/" + path2);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent2 = Intent.createChooser(intent, "Open File");
try {
startActivity(intent2);
} catch (ActivityNotFoundException e) {
Toast.makeText(ListviewActivity.this, "No pdf viewer found. Install one. ", Toast.LENGTH_LONG).show();
}
}
}
}
});
}
}
公共类ListviewActivity扩展活动
{
公共类数据适配器扩展了ArrayAdapter
{
语境;
国际资源布局;
ArrayList数据=null;
公共数据_适配器(上下文c、int r、ArrayList dc)
{
超级(c、r、dc);
this.ResourceLayoutId=r;
this.context=c;
这个数据=dc;
}
公共视图getView(int位置、视图转换视图、视图组父视图)
{
视图行=转换视图;
数据持有者=null;
if(行==null)
{
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
行=充气机。充气(ResourceLayoutId,父级,false);
holder=新数据持有者();
holder.image=(ImageView)row.findViewById(R.id.image1);
holder.txt=(TextView)row.findViewById(R.id.textlist);
row.setTag(支架);
}否则
{
holder=(DataHolder)row.getTag();
}
Data_Class dc=Data.get(位置);
holder.txt.setText(dc.data);
位图bm=decodeSampledBitmapFromUri(data.get(position.get_path(),100100);
holder.image.setImageBitmap(bm);
返回行;
}
公共位图解码SampleDbitMapFromUri(字符串路径、int-reqWidth、int-reqHeight)
{
位图bm=null;
//使用INJUSTDECBOUNDS首次解码=true检查尺寸
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
解码文件(路径、选项);
//计算样本大小
options.inSampleSize=计算样本大小(options、reqWidth、reqHeight);
//使用inSampleSize集合解码位图
options.inJustDecodeBounds=false;
bm=BitmapFactory.decodeFile(路径、选项);
返回bm;
}
公共整数计算示例大小(
BitmapFactory.Options选项、int reqWidth、int reqHeight)
{
//图像的原始高度和宽度
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>要求高度| |宽度>要求宽度)
{
如果(宽度>高度)
{
inSampleSize=数学圆((浮动)高度/(浮动)要求高度);
}否则
{
inSampleSize=数学圆((浮动)宽度/(浮动)宽度);
}
}
返回样本大小;
}
公共类数据持有者
{
图像视图图像;
文本视图;
}
}
私有列表视图列表视图1;
私有整数检查视图;
私有文件targetDirectory;
私有文件[]文件;
受保护的静态ArrayList dataclass=新ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
//TODO自动生成的方法存根
检查_view=0;
super.onCreate(savedInstanceState);
setContentView(R.layout.listview\u布局);
listview1=(ListView)findViewById(R.id.List1);
字符串路径=Environment.getExternalStorageDirectory().getAbsolutePath();
字符串targetPath=path+“/MyApp/”;
targetDirectory=新文件(targetPath);
files=targetDirectory.listFiles();
对于(int i=0;i
//包含imageview和textview的image.xml
<ImageView
android:id="@+id/image1"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
/>
<TextView
android:id="@+id/textlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="15dp"
android:textStyle="bold" />
//和main activity.xml
<ListView
android:id="@+id/List1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
您的列表视图包含
数据类的列表
而不是字符串
,这会导致崩溃,所以请更改
String path = (String) parent.getItemAtPosition(position);
到
使用
数据类文件名变量更改名称
变量 您正试图将数据\u Class
强制转换为字符串
,这可能会导致类异常
。该异常有效。我有另一个问题,当我在listview中单击项目时,同一项目在listview中显示多次。为什么?任何ideas@artist您是否多次单击?它在listview或Toast中显示了多次?否,我只单击了一次,它将在listview中倍增并显示相同的数据另一件事,在listview的onclick事件中,我检查文件是否为pdf,然后打开
Data_Class mData_Class = (Data_Class) parent.getItemAtPosition(position);
String path = mData_Class.Name;