How to create Customize adapter for ListView where Image and 2 Text\View contain in an Item and on its item click show Name of selected Item?
I have searched for this my bad luck no result found according to me. Please help.
- 6 years ago
activity_main.xml
Below is the main layout file which contains the listview.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView_ID"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
String names[] = {"red", "green", "blue", "yellow", "pink", "brown"};
Integer image[] = {R.drawable.ic_dot_red, R.drawable.ic_dot_green, R.drawable.ic_dot_blue, R.drawable.ic_dot_yellow, R.drawable.ic_dot_pink, R.drawable.ic_dot_brown};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomListViewAdapter adapter = new CustomListViewAdapter(this, image, names);
ListView listView = (ListView) findViewById(R.id.listView_ID);
listView.setAdapter(adapter);
}
class CustomListViewAdapter extends ArrayAdapter<String>{
String[] _names ;
Integer[] _image ;
Context context;
public CustomListViewAdapter (Activity context,Integer[] image_id, String[] text){
super(context, R.layout.list_row, text);
this._names = text;
this._image = image_id;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflateView= inflater.inflate(R.layout.list_row, null,true);
TextView textView = (TextView) inflateView.findViewById(R.id.textView);
ImageView imageView = (ImageView) inflateView.findViewById(R.id.imageView);
textView.setText(_names[position]);
imageView.setImageResource(_image[position]);
return inflateView;
}
}
}
Hot Questions