簡體   English   中英

Android-如何將項目放入帶有Hashmap和List的ListView中

[英]Android - How to put a item click in ListView with Hashmap and List

在我的應用程序中,我使用一個名為GetImageTaskAsyncTask從具有用戶指定的int的URL獲取圖像。

示例:url =“ http://example.com/image.php?index= ” + int

我將圖像放在ImageView並在"doInBackground" ,為兩個列表中的add元素創建了兩個HashMap

第一個是當我單擊按鈕"onClickIndex"時在ListView顯示索引列表。

第二個是使用給定的索引保存我的Image,因為我希望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();
    }
   }


  }

抱歉,我沒有盡快回復您,但我很忙。

不要使用哈希圖存儲位圖。 事情變得復雜了。

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

然后更改此行。

Bitmap Imageback = list2.get(position).get(R.id.index);

對此

Bitmap ImageBack = list2.get(position);

您寫的內容行不通的原因是您沒有將圖像存儲在key:R.id.index中

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

R.id.index是R生成的整數,它在應用運行之前由R生成,它唯一地標識帶有該標簽的任何內容。 這就是為什么它不接受(據我所知)的原因

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM