Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android-如何使用Hashmap和List在ListView中单击项目_Java_Android_Listview_Android Listview - Fatal编程技术网

Java Android-如何使用Hashmap和List在ListView中单击项目

Java Android-如何使用Hashmap和List在ListView中单击项目,java,android,listview,android-listview,Java,Android,Listview,Android Listview,在我的应用程序中,我使用名为GetImageTask的AsyncTask从URL获取图像,其中包含用户给定的int 示例:url=”“+int 我将图像放在ImageView中,并在的“doInBackground”中,为两个列表中的add元素创建两个HashMap 第一个是当我点击按钮“onClickIndex”时,在我的列表视图中显示索引列表 第二个用于使用给定的索引保存我的图像,因为我希望ListView的元素在用户单击图像时返回图像(onClickIndex) 但是我在这一部分被阻止了,

在我的应用程序中,我使用名为
GetImageTask
AsyncTask
URL
获取图像,其中包含用户给定的int

示例:url=”“+int

我将图像放在
ImageView
中,并在
的“doInBackground”
中,为两个列表中的add元素创建两个
HashMap

第一个是当我点击按钮“onClickIndex”时,在我的
列表视图中显示索引列表

第二个用于使用给定的索引保存我的图像,因为我希望
ListView
的元素在用户单击图像时返回图像(
onClickIndex

但是我在这一部分被阻止了,因为我无法在第二个列表中获得带有索引的位图,并且不知道该怎么做

MainActivity.java

public class MainActivity extends ActionBarActivity {
private EditText MyNumber;
private ImageView MyImage;
private ListView MyIndexList;
private ArrayList<HashMap<String,String>> list = new ArrayList<>();
private ArrayList<HashMap<Integer,Bitmap>> list2 = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MyNumber = (EditText)findViewById(R.id.choiceofindex);
    MyImage = (ImageView)findViewById(R.id.imageRecup);
    MyIndexList = (ListView)findViewById(R.id.listIndex);

}

class GetImageTask extends AsyncTask<Integer,Integer,Bitmap> {

    protected Bitmap doInBackground(Integer... index) {
        HashMap<String,String> hashmap = new HashMap<>();
        HashMap<Integer,Bitmap> hashmap2 = new HashMap<>();
        Integer choix = index[0];
        String urldisplay = "http://example.com/image.php?index=" + choix;
        Bitmap RecupImage = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            RecupImage = BitmapFactory.decodeStream(in);
        }
        catch (Exception e){
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        hashmap.put("index","index " +choix);
        hashmap2.put(choix,RecupImage);
        list.add(hashmap);
        list2.add(hashmap2);
        return RecupImage;
    }

    protected void onProgressUpdate(Integer... progress)
    {
        Context context = getApplicationContext();
        super.onProgressUpdate(progress);
        Toast.makeText(context, "Wait...", Toast.LENGTH_LONG).show();

    }

    protected void onPostExecute(Bitmap result)
    {
        Context context = getApplicationContext();
        super.onPostExecute(result);
        MyImage.setImageBitmap(result);
        Toast.makeText(context, "Image!!", Toast.LENGTH_LONG).show();
    }
}


public void onClickRecup(View v)
{
    try{
        int index= Integer.parseInt(MyNumber.getText().toString());
        GetImageTask GetImage = new GetImageTask();
        GetImage.execute(index);
    }
    catch (NumberFormatException nfe) {
        Context context = getApplicationContext();
        Toast.makeText(context, "No image!", Toast.LENGTH_LONG).show();
    }
}


public void onClickIndex(View v)
{
    try {
        SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.row, new String[]{"index"},
                new int[] {R.id.index});
        MyIndexList.setAdapter(adapter);
        MyIndexList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bitmap Imageback = list2.get(position).get(R.id.index);
                MyImage.setImageBitmap(Imageback);
            }
        });
    }
    catch (Exception e){
        Context context = getApplicationContext();
        Toast.makeText(context, "Problem on ListView", Toast.LENGTH_LONG).show();
    }
   }


  }
公共类MainActivity扩展了ActionBarActivity{
私人编辑文本MyNumber;
私有图像查看我的图像;
私有ListView MyIndexList;
private ArrayList list=new ArrayList();
private ArrayList list2=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyNumber=(EditText)findViewById(R.id.ChoiceOffIndex);
MyImage=(ImageView)findViewById(R.id.imageRecup);
MyIndexList=(ListView)findViewById(R.id.listIndex);
}
类GetImageTask扩展了AsyncTask{
受保护位图doInBackground(整数…索引){
HashMap HashMap=新的HashMap();
HashMap hashmap2=新的HashMap();
整数choix=索引[0];
字符串URL显示=”http://example.com/image.php?index=“+choix;
位图RecipImage=null;
试一试{
InputStream in=newjava.net.URL(urldisplay.openStream();
RecupImage=BitmapFactory.decodeStream(in);
}
捕获(例外e){
Log.e(“Error”,e.getMessage());
e、 printStackTrace();
}
hashmap.put(“索引”、“索引”+choix);
hashmap2.put(choix,RecupImage);
list.add(hashmap);
列表2.add(hashmap2);
回归回归年龄;
}
受保护的void onProgressUpdate(整数…进度)
{
Context=getApplicationContext();
super.onProgressUpdate(进度);
Toast.makeText(上下文,“Wait…”,Toast.LENGTH_LONG.show();
}
受保护的void onPostExecute(位图结果)
{
Context=getApplicationContext();
super.onPostExecute(结果);
设置图像位图(结果);
Toast.makeText(上下文,“Image!!”,Toast.LENGTH\u LONG.show();
}
}
公共void onClickRecup(视图五)
{
试一试{
int index=Integer.parseInt(MyNumber.getText().toString());
GetImageTask GetImage=新建GetImageTask();
执行(索引);
}
捕获(NumberFormatException nfe){
Context=getApplicationContext();
Toast.makeText(上下文,“无图像!”,Toast.LENGTH_LONG.show();
}
}
公共索引(视图v)
{
试一试{
simpledapter adapter=newsimpledapter(this,list,R.layout.row,新字符串[]{“index”},
新的int[]{R.id.index});
设置适配器(适配器);
MyIndexList.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
位图Imageback=list2.get(位置).get(R.id.index);
设置图像位图(Imageback);
}
});
}
捕获(例外e){
Context=getApplicationContext();
Toast.makeText(上下文,“ListView上的问题”,Toast.LENGTH_LONG.show();
}
}
}

对不起,我没能早点给你回电话,但我很忙

不要使用哈希映射来存储位图。事情太复杂了

ArrayList<Bitmap> list2 = new ArrayList<>();
对此

Bitmap ImageBack = list2.get(position);
您编写的代码不起作用的原因是您没有将图像存储在key:R.id.index上

Integer choix = index[0];
...
hashmap2.put(choix,RecupImage);

R.id.index是在运行应用程序之前由R生成的整数,它唯一地标识具有该标签的任何内容。这就是为什么它以前不接受它(据我所知)

老实说,我很好奇。这在某种程度上编译/运行了吗?是的,我可以在ListView中获得图像和索引列表。我只是不知道如何将索引(在列表中)与相关位图(在列表2中)连接起来。好的。这太复杂了。在我回答这个问题之前,让我先问一下:出于任何特殊原因,您必须使用hashmaps吗?不,我只是使用它们来保存我的两个列表中的元素。问题-在这样的场景中,RecyclerView如何工作?非常感谢您的解决方案和解释。它工作得很好!还有一个小问题,当我从url下载我的图像时,我会在下载完成后收到消息“image!!”,而不是“onProgressUpdate”中的“wait…”消息,我不知道为什么?@Tenebros通常使用getApplicationContext()是可以避免的,我想这可能是你的问题。试试这个:main activity.thisOk我回家后会测试这个。什么都不会发生,但我找到了在“doInBackground”中用publishProgress()调用“onProgressUpdate”的解决方案,土司就会出现。再次感谢你,伊斯玛斯维扎德!
Integer choix = index[0];
...
hashmap2.put(choix,RecupImage);