gpt4 book ai didi

java - 可点击图像的 ListView ,无法打开其他 Activity

转载 作者:行者123 更新时间:2023-12-02 02:54:44 24 4
gpt4 key购买 nike

[已编辑]我想制作图像 ListView ,单击它们后应该打开另一个 Activity (每个可单击的单独 Activity ),但 setOnItemClickListener 不起作用。

这是我的主要 Activity.java

package com.example.priyanshu.justforfun;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


final ArrayList<Block> blocks = new ArrayList<Block>();

blocks.add(new Block(R.drawable.one,R.drawable.two));
blocks.add(new Block(R.drawable.three,R.drawable.four));
blocks.add(new Block(R.drawable.five,R.drawable.six));
blocks.add(new Block(R.drawable.seven,R.drawable.eight));
blocks.add(new Block(R.drawable.nine,R.drawable.ten));

ImageAdapter adapter = new ImageAdapter(MainActivity.this, blocks, R.color.gray);

ListView listView = (ListView) findViewById(R.id.list);

listView.setAdapter(adapter);


listView.setOnItemClickListener(
new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(MainActivity.this,DemoActivity.class);
startActivity(intent);
}
}
);

这是Imageadapter.java-

public class ImageAdapter extends ArrayAdapter<Block> {

private int mColorResourceId;

public ImageAdapter(Activity context, ArrayList<Block> words, int colorResourceId) {
super(context, 0, words);
mColorResourceId = colorResourceId;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}

Block currentBlock = getItem(position);

ImageButton iconView1 = (ImageButton) listItemView.findViewById(R.id.im1);
ImageButton iconView2 = (ImageButton) listItemView.findViewById(R.id.im2);

iconView1.setImageResource(currentBlock.getImage1ResourceId());
iconView2.setImageResource(currentBlock.getImage2ResourceId());

return listItemView;
}

}

这是Block.java-

public class Block {

private int mImage1ResourceId = NO_IMAGE_PROVIDED;
private int mImage2ResourceId = NO_IMAGE_PROVIDED;

private static final int NO_IMAGE_PROVIDED = -1;

public Block(int Image1ResourceId ,int Image2ResourceId) {
mImage1ResourceId = Image1ResourceId;
mImage2ResourceId = Image2ResourceId;
}

public int getImage1ResourceId(){ return mImage1ResourceId;}

public int getImage2ResourceId(){ return mImage2ResourceId;}
}

这是List_item.xml

    xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/list_item">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="centerCrop"
android:src="@drawable/background_splash"
android:id="@+id/im1"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="centerCrop"
android:src="@drawable/background_splash"
android:id="@+id/im2" />
</LinearLayout>

最佳答案

正如 CommonsWare 所说:

You are trying to allocate 74649612 bytes. This is equivalent to a 8640 x 8640-pixel image. This is much too large.

您必须在可绘制对象中使用小得多的图像,因为这会分配太多内存。此外,您可以使用Glide/Picasso加载图像。Glide有效处理位图解码、磁盘缓存,以确保更少的内存分配并避免内存不足错误。

为了在您的项目中使用 Glide,首先,将其添加到应用程序 gradle 文件中的依赖项:

repositories {
mavenCentral() // jcenter() works as well because it pulls from Maven Central
}

dependencies {

compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:xx.x.x' // According to your compileSdkVersion

}

在您的getView()内部:

替换:

 iconView1.setImageResource(currentBlock.getImage1ResourceId());
iconView2.setImageResource(currentBlock.getImage2ResourceId());

与:

 Glide.with(context)
.load(currentBlock.getImage1ResourceId())
.placeholder(R.drawable.placeholder) //Use low res image as placeholder
.error(R.drawable.imagenotfound) //Use low res image as error image
.into(iconView1);

Glide.with(context)
.load(currentBlock.getImage2ResourceId())
.placeholder(R.drawable.placeholder)
.error(R.drawable.imagenotfound)
.into(iconView2);

要在单击不同的项目时打开不同的 Activity ,请使用以下代码:

gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {

if(position == 0){
Intent i = new Intent(MainActivity.this, NextActivity1.class);
startActivity(i);
}
else if(position == 1){
Intent i = new Intent(MainActivity.this, NextActivity2.class);
startActivity(i);
}
// Do as above for rest of the positions
}
});

关于java - 可点击图像的 ListView ,无法打开其他 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43297566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com