0

I need to modify my code to make it show the text with image not to change the whole code to another one with image function:

This is my code for images:

 int[] imgs = new int[]
    {
        R.drawable.wifi,
        R.drawable.bluetooth,
        R.drawable.usb,
        R.drawable.cloud,
        R.drawable.remote,

    };

And this is my ListView code:

listView = (ListView) findViewById(R.id.operations);
listView.setTextFilterEnabled(true);
String[] values = new String[]
        {"Nearby Wifi",
         "Nearby Blutooth",
         "Direct USB Connected",
         "Google Cloud Print",
         "Printer Remote"
        };

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        R.layout.custom_listview, android.R.id.text1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {


        int itemPosition = position;

        String itemValue = (String)   listView.getItemAtPosition(position);

        //if condition
    }


});

And this is my custom_listview:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:paddingTop="2dip"
android:paddingBottom="3dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="@android:color/black"
android:textStyle="bold"
  />

How to modify it to show the text with the image?

gvlasov
  • 18,638
  • 21
  • 74
  • 110
hani kamal
  • 55
  • 1
  • 7

2 Answers2

0

You need to customize listview, for that you need

  1. Custom listview row xml (Imageview & Textview)
  2. Custom base adapter

Follow this link, http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=9

Ganesh AB
  • 4,652
  • 2
  • 18
  • 29
0

The view that come back on your adapter is a TextView. What you can do is use setCompoundDrawables

Example: Edit: ...

    listView.setAdapter(new BaseAdapter() {
        @Override
        public int getCount() {
            return values.size();
        }

        @Override
        public Object getItem(int position) {
            return values.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            if(convertView == null) {
                convertView = inflater.inflate(R.layout.custom_textview);

                TextView tv = (TextView) convertView.findViewById(R.id.text1);
                tv.setCompoundDrawables(getResources().getDrawable(imgs[position]), null, null, null);
tv.setText(values.get(position));
            }

            return convertView;
        }
    });
Marcus Gabilheri
  • 1,259
  • 1
  • 14
  • 26
  • android can't resolve :context and setCompoundDrawables – hani kamal Feb 28 '15 at 18:19
  • The context is whatever your context is... if you are inside an activity you don't need to do put context, if you are in a fragment use getActivity(), etc... For the setCompoundDrawables you need to cast it to a (TextView)... I have edited my answer with the code. – Marcus Gabilheri Feb 28 '15 at 18:21
  • excuse me , i'm newly on android , and i shown all the activity in my question, and this is layout – hani kamal Feb 28 '15 at 18:24
  • can't resolve "context", so i have to replace it with what ?! – hani kamal Feb 28 '15 at 18:32
  • I edited my answer with a copy and paste solution. I also recomend you to read this: http://stackoverflow.com/questions/3572463/what-is-context-in-android – Marcus Gabilheri Feb 28 '15 at 18:37