English 中文(简体)
名单/行文中动态背景图像
原标题:Dynamic background image on a TextView from a ListView/ row

页: 1 页: 1 我有大约18种不同的图像,我想视每个项目/行的类别展示。 图像名称为abc1",abc2"等。 这里是我的习惯代码<代码>。 CursorAdapter :

  private class MyListAdapter extends SimpleCursorAdapter {

    public MyListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
        super(context, layout , cursor, from, to);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        // Get the resource name and id
        String resourceName = "R.drawable.abc" + cursor.getString(7);
        int resid = context.getResources().getIdentifier(resourceName,"drawable",context.getPackageName());

        // Create the idno textview with background image
        TextView idno = (TextView) view.findViewById(R.id.idno);
        idno.setText(cursor.getString(3));
        idno.setBackgroundResource(resid);

        // create the material textview
        TextView materials = (TextView) view.findViewById(R.id.materials);
        materials.setText(cursor.getString(1)); 
    }
}

当我行走时,resid <<0>>0>,其中表示没有找到资源。 。 参看正确, id:R.drawable.abc1"。 我将图像文件输入res/drawable,并将其列入R.java

这样做的正确途径是,还是谁有更好的解决办法?

最佳回答

您不使用全名,例如R.drawable.abc1,你只使用drawable后的名称。 <代码>getIdentifier () 用于建立正确的<代码>String, 载于<代码> > 姓名/代码>、<代码> 类型和<编码>>>>。 因此,应当使用<代码>getIdentifier():

String resourceName = "abc" + cursor.getString(7);
int resid = context.getResources().getIdentifier(resourceName,"drawable",context.getPackageName());

此外,你应研究另一种将图像作为背景的方法,因为getIdentifier(>)是执行方法的较慢的方法,将在bindView<>/code>上注明。 可称为用户浏览<代码>ListView的多次反馈(有时用户可以快速这样做)。

http://www.un.org。

您能够更有效地使用<代码>getIdentifier的一种方式,是将胎中的胎体放入“条码”。 CursorAdapter and Store in a HashMap<Integer, Integer>. themap between the number in the name (abc 1, abc 2等) and the actual id:

private HashMap<Integer, Integer> setUpIdsMap() {
     HashMap<Integer, Integer> mapIds = new HashMap<Integer, Integer>();
     // I don t know if you also have abc0, if not use this and substract 1 from the cursor value you get
     for (int i = 0; i < 18; i++) {
         String resourceName = "abc" + 0;
         int resid =       context.getResources().getIdentifier(resourceName,"drawable",context.getPackageName());
         mapIds.put(i, resid);
     iii

iii

在适应者建造者中:

//...field in your adapter class
    HashMap<Integer, Integer> ids = new HashMap<Integer, Integer>();

//in the constructor:
//...
    ids = setUpIdsMap();  
//...

然后在<代码>bindView上。 方法使用以下方式归还的阵列:

//...
 // Create the idno textview with background image
        TextView idno = (TextView) view.findViewById(R.id.idno);
        idno.setText(cursor.getString(3));
        idno.setBackgroundResource(ids.get(cursor.getString(7)));//depending if you have abc0 or not you may want to substract 1 from cursor.getString(7) to match the value from the setUpIdsMap method
//...
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签