Friday 2 August 2013

Image ListView in Android

1 comment
Today I will discussed how to make Image ListView in android. I have made a demo project named ImageListView.
Step 1:- create activity_main.xml,  in this xml I have defined how image and text will come with respective to each other.
Step 2:- the image used in the project is in the drawable folder. Please put image in your drawable folder before you are going to refer them.
Step 3:- This is how my main.java looks

package com.example.imagelistview;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class Main extends ListActivity implements OnItemClickListener {
    String []proList ={"Android", "C", "C++", "Java", "JavaScript", "Jquery", "Objective C", "iOS"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setting up an adapter and passing an array and context of this class        setListAdapter(new MyListAdapter(this,proList));
       
        ListView listView = getListView();
        listView.setOnItemClickListener(this);
       
    }
    @Override
    public void onItemClick(AdapterView<?> adapter, View arg1, int position, long arg3) {
         
        String str = adapter.getItemAtPosition(position).toString();
        Toast.makeText(getBaseContext(), "You Clicked on: "+str, Toast.LENGTH_SHORT).show();
       
    }
}   

Step 4:- Setting up an Adapter named MyListAdapter.java
package com.example.imagelistview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyListAdapter extends ArrayAdapter<String>{
   
    private String []values;
    private Context context;
   
    public MyListAdapter(Context context, String []values) {
        // TODO Auto-generated constructor stub
        super(context,R.layout.activity_main,values);
        this.context = context;
        this.values = values;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
       
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       
        View rowView = inflater.inflate(R.layout.activity_main, parent,false);
       
        TextView textV = (TextView)rowView.findViewById(R.id.textView1);
        ImageView imgV = (ImageView)rowView.findViewById(R.id.imageView1);
       
        textV.setText(values[position]);
       
        String str =values[position];
       
            if(str.equalsIgnoreCase("android")){
                imgV.setImageResource(R.drawable.ic_launcher);
            }else
                if(str.equalsIgnoreCase("c")){
                    imgV.setImageResource(R.drawable.c);
                }else
                    if(str.equalsIgnoreCase("c++")){
                        imgV.setImageResource(R.drawable.cplus);
                    }else
                        if(str.equalsIgnoreCase("Java")){
                            imgV.setImageResource(R.drawable.java);
                        }else
                            if(str.equalsIgnoreCase("javascript")){
                                imgV.setImageResource(R.drawable.javascript);
                            }else
                                if(str.equalsIgnoreCase("jquery")){
                                    imgV.setImageResource(R.drawable.jquery);
                                }else
                                    if(str.equalsIgnoreCase("objective c")){
                                        imgV.setImageResource(R.drawable.objectivec);
                                    }else
                                        if(str.equalsIgnoreCase("ios")){
                                            imgV.setImageResource(R.drawable.ios);
                                        }
               
       
        return rowView;
    }
}

1 comment:

  1. Casinos in the UK - How to find good games - GrizzGo
    So, what https://sol.edu.kg/ do we mean by “casinos in the UK”? ford escape titanium to find a casino and live gri-go.com casino games febcasino.com on a https://vannienailor4166blog.blogspot.com/ mobile phone device in 2021.

    ReplyDelete