2017-03-17 59 views
0
public class UserlistActivity extends AppCompatActivity { 

    static ListView userlist; 
    static String[] users; 
    ImageView user; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_userlist); 
     setIDs(); 
     Bitmap userphoto = null; 
     try { 
      userphoto = getBitmapFromAsset("user dashboard.png"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     new Gate(this, "USERLIST").execute(); 
     String mDrawableName = "user dashboard"; 
     int resID = getResources().getIdentifier(mDrawableName , "assets", getPackageName()); 
     user.setImageResource(resID); 
     user.setImageBitmap(userphoto); 
    } 

    private Bitmap getBitmapFromAsset(String strName) throws IOException 
    { 
     AssetManager assetManager = getAssets(); 
     InputStream istr = assetManager.open(strName); 
     Bitmap bitmap = BitmapFactory.decodeStream(istr); 
     return bitmap; 
    } 

    private void setIDs() { 
     userlist = (ListView) findViewById(R.id.userlist); 
     View inflatedView = getLayoutInflater().inflate(R.layout.userlist_list_item, null); 
     user = (ImageView) inflatedView.findViewById(R.id.user); 
    } 

    static void fillData(Context context) { 
     ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, R.layout.userlist_list_item, R.id.listtext, users); 
     userlist.setAdapter(arrayAdapter); 
    } 

這裏我試圖getbitmap(這是在其他活動中工作正常),並將它附加到圖像視圖,這是我傳遞給適配器的列表項自定義佈局的一部分,但我不知道在哪裏問題,圖像不會出現爲什麼我的imageview不採取位圖並顯示它?

編輯::

我加入照片的另一個副本繪製這樣我就可以添加源並跳過努力

+0

你發現任何異常? – rafsanahmad007

+0

資產中是否沒有任何子文件夾? –

+0

沒有例外,沒有子文件夾,這是讓我瘋狂的 –

回答

1

我覺得它固定您忘了將inflatedView添加到您的parent視圖

使用此代碼:

LinearLayout parent = (LinearLayout)findViewById(R.id.parent_layout); 
View inflatedView = getLayoutInflater().inflate(R.layout.userlist_list_item, null); 
user = (ImageView) inflatedView.findViewById(R.id.user); 
parent.addView(inflatedView); 

使用下面的方法:

private Bitmap getBitmapFromAsset(String strName) 
{ 
    AssetManager assetManager = getAssets(); 
    InputStream istr = null; 
    try { 
     istr = assetManager.open(strName); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    Bitmap bitmap = BitmapFactory.decodeStream(istr); 
    return bitmap; 
} 
+0

抱歉,但我如何獲得父佈局的編號? –

+0

它是你的'activity_userlist'中的父級佈局...你想添加視圖的地方 – rafsanahmad007

+0

它將圖像添加到列表中,但在他們的地方沒有圖像(每個列表行中的圖像) –