2017-03-17 61 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

它将图像添加到列表中,但在他们的地方没有图像(每个列表行中的图像) –