firstly I made a listview then I want to make listview with icons. I do it for;(This is my custom adapter and of course I have a xml for it. It is listeozellikleri.xml)
public class listeozellikleri extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public listeozellikleri(Context context, String[] values) {
super(context, R.layout.liste_ozellikleri, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.liste_ozellikleri, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.logoisim);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logosimge);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
if (s.equals("photo1")) {
imageView.setImageResource(R.drawable.image1);
} else if (s.equals("photo2")) {
imageView.setImageResource(R.drawable.image2);
} else if (s.equals("photo3")) {
imageView.setImageResource(R.drawable.image3);
} else if (s.equals("photo4")) {
imageView.setImageResource(R.drawable.image4);
}
return rowView;
}
}
And this is my main listview;
TextView dogrusonuc;
TextView bossonuc;
ListView sonuclistesi;
Context context;
//logo verileri icin adaptor acma baslangic
ArrayList<String> veriler; {
veriler = new ArrayList<String>();
}
//logo verileri icin adaptor acma bitis
#Override
protected void onCreate(Bundle savedInstanceState) {
//tam ekran kodu baslangic
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//tam ekran kodu bitis
super.onCreate(savedInstanceState);
setContentView(R.layout.sonuclar);
//logo verileri cekme baslangic
Intent veri = getIntent();
veriler = veri.getStringArrayListExtra("logoveri");
//logo verileri cekme bitis
//liste olusturma baslangic
ListView sonuclistesi=(ListView) findViewById(R.id.sonuclistesi);
ArrayAdapter<String> veriadaptoru=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, veriler);
sonuclistesi.setAdapter(veriadaptoru);
//liste olusturma bitis
//sonuclari yazdirma baslangic
dogrusonuc = (TextView)findViewById(R.id.dogrusonuc);
dogrusonuc.setText("doğru sayınız: " + getIntent().getExtras().getString("dogruveri"));
bossonuc = (TextView)findViewById(R.id.bossonuc);
bossonuc.setText("boş sayınız: " + getIntent().getExtras().getString("bosveri"));
//sonuclari yazdirma bitis
}
private AbsListView sonuclistesi() {
// TODO Auto-generated method stub
return null;
}
}
How can I connect custom adapter to main listview? Because I need to show datas with icons...
*When my intent add photo1 to my listview then image1 will be this item's icon...
Instead of this -
ArrayAdapter veriadaptoru=new ArrayAdapter(this, android.R.layout.simple_list_item_1, veriler);
sonuclistesi.setAdapter(veriadaptoru);
Use below.
listeozellikleri mAdapter = new listeozellikleri(this,veriler);
sonuclistesi.setAdapter(veriadaptoru);
And it is good practice to have class names starting in caps. so listeozellikleri should be Listeozellikleri.
Also if veriler is an ArrayList, you should change String[] values in your adapter to ArrayList values.
Change your adapter as below
public class listeozellikleri extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String> values;
public listeozellikleri(Context context, ArrayList<String> values) {
super(context, R.layout.liste_ozellikleri, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.liste_ozellikleri, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.logoisim);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logosimge);
textView.setText(values.get(position));
// Change icon based on name
String s = values.get(position);
System.out.println(s);
if (s.equals("photo1")) {
imageView.setImageResource(R.drawable.image1);
} else if (s.equals("photo2")) {
imageView.setImageResource(R.drawable.image2);
} else if (s.equals("photo3")) {
imageView.setImageResource(R.drawable.image3);
} else if (s.equals("photo4")) {
imageView.setImageResource(R.drawable.image4);
}
return rowView;
}
}
Related
Hi guys I am implementing a listview with a clickable button through this tutorial -> https://www.youtube.com/watch?v=ZEEYYvVwJGY
and I want to achieve the same output.
But I have a slight problem, since my list view is populated from mysql database but in the tutorial is not populated from mysql database so we have a different adapter.
Codes in the tutorial
public class MainActivity extends AppCompatActivity{
private ArrayList<String> data = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendance);
ListView lv = (ListView) findViewById(R.id.listview);
lv.setAdapter(new MyListAdapter(this, R.layout.list_item, data));
}
}
Then lets assume that this is the tutorials MyListAdapter
private class MyListAdapter extends ArrayAdapter<String>{
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if (convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.tvid = (TextView) convertView.findViewById(R.id.studId);
viewHolder.tvname = (TextView) convertView.findViewById(R.id.studName);
viewHolder.btnP = (Button) convertView.findViewById(R.id.present);
viewHolder.btnA = (Button) convertView.findViewById(R.id.absent);
viewHolder.btnP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(),"Button Clicked " + position, Toast.LENGTH_LONG).show();
}
});
convertView.setTag(viewHolder);
}
return convertView;
}
}
and my problem is in this part
the setAdapter
mylistView = (ListView) findViewById(R.id.list);
final ListAdapter adapter = new SimpleAdapter(Attendance.this,
studentList, R.layout.list_att, new String[]{
TAG_ID, TAG_NAME}, new int[]{
R.id.studId, R.id.studName});
mylistView.setAdapter(adapter);
what should I do? any help would be appreciated thanks in advance :)
this is my code
public class Attendance extends AppCompatActivity {
TextView Date;;
ListView mylistView;
private ArrayList<HashMap<String, String>> studentList = new ArrayList<HashMap<String, String>>();
TextView Name;
private static String url = "http://10.0.2.2/MobileClassRecord/getStudent.php";
private static final String TAG_STUDENTS = "students";
private static final String TAG_ID = "stud_id";
private static final String TAG_NAME = "stud_name";
JSONArray students = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendance);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Date = (TextView) findViewById(R.id.tvDate);
final Calendar calendar = Calendar.getInstance();
int dd = calendar.get(Calendar.DAY_OF_MONTH);
int mm = calendar.get(Calendar.MONTH);
int yy = calendar.get(Calendar.YEAR);
Date.setText(new StringBuilder().append(yy).append("-").append(mm + 1).append("-").append(dd));
new JSONParse().execute();
studentList = new ArrayList<HashMap<String, String>>();
}
private class JSONParse extends AsyncTask<String, String, JSONObject>{
private ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
Name =(TextView) findViewById(R.id.studName);
pDialog = new ProgressDialog(Attendance.this);
pDialog.setMessage("Getting Data from Database...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected JSONObject doInBackground(String... params) {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
#Override
protected void onPostExecute(JSONObject jsonObject) {
pDialog.dismiss();
try {
students = jsonObject.getJSONArray(TAG_STUDENTS);
for (int i = 0; i < students.length(); i++){
JSONObject c =students.getJSONObject(i);
final String Id = c.getString(TAG_ID);
String Name = c.getString(TAG_NAME);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, Id);
map.put(TAG_NAME, Name);
studentList.add(map);
mylistView = (ListView) findViewById(R.id.list);
final ListAdapter adapter = new SimpleAdapter(Attendance.this,
studentList, R.layout.list_att, new String[]{
TAG_ID, TAG_NAME}, new int[]{
R.id.studId, R.id.studName});
mylistView.setAdapter(adapter);
mylistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String ID = ((TextView) (view.findViewById(R.id.studId))).getText().toString();
Toast.makeText(Attendance.this, "List Clicked " + ID, Toast.LENGTH_LONG).show();
SharedPreferences preferences = getSharedPreferences("MyApp", MODE_PRIVATE);
preferences.edit().putString("id", ID).commit();
}
});
}
}catch (JSONException e){
e.printStackTrace();
}
}
}
private class MyListAdapter extends ArrayAdapter<String>{
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if (convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.tvid = (TextView) convertView.findViewById(R.id.studId);
viewHolder.tvname = (TextView) convertView.findViewById(R.id.studName);
viewHolder.btnP = (Button) convertView.findViewById(R.id.present);
viewHolder.btnA = (Button) convertView.findViewById(R.id.absent);
viewHolder.btnP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(),"Button Clicked " + position, Toast.LENGTH_LONG).show();
}
});
convertView.setTag(viewHolder);
}
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.tvid.setText(getItem(position));
return convertView;
}
}
public class ViewHolder{
TextView tvid, tvname;
Button btnP, btnA;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_attendance, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
Intent intent = new Intent(Attendance.this, SpecificClassRecord.class);
startActivity(intent);
return true;
case R.id.action_view_attendance:
Intent intent1 = new Intent(Attendance.this, ViewAttendance.class);
startActivity(intent1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Sample Layout
like Udi said, more information would be nice.
One strange thing in your code is in the getView() method.
you have
if(convertView==null){
...
}else{
...
}
I think the else part is wrong. Just remove the else and the brackets (not the code insight of it).
The code in the else part is important, because without it the code will not change the TextView when the View is loaded for the first time. This mean, that your getView will do nothing
EDIT
The author has changed the code. You have now two different adapters there...
In my opinion the getView() Method should look like this
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if (convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.tvid = (TextView) convertView.findViewById(R.id.studId);
viewHolder.tvname = (TextView) convertView.findViewById(R.id.studName);
viewHolder.btnP = (Button) convertView.findViewById(R.id.present);
viewHolder.btnA = (Button) convertView.findViewById(R.id.absent);
viewHolder.btnP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(),"Button Clicked " + position, Toast.LENGTH_LONG).show();
}
});
convertView.setTag(viewHolder);
}
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.tvid.setText(getItem(position));
return convertView;
}
}
But for me it is still not clear where exactly the problem is. Is the App crashing or is there just a behavior you don't understand? If yes, what exactly?
If the App is crashing, when exactly?
EDIT 2:
mylistView = (ListView) findViewById(R.id.list);
final ListAdapter adapter = new MyListAdapter(Attendance.this,
R.layout.list_att, new String[]{
TAG_ID, TAG_NAME});
mylistView.setAdapter(adapter);
maybe this is all. I assume the layout of your rows is given by the layoutressource R.layout.list_att and that TAG_ID and TAG_Name are defined somewhere before.
I see your new Adapter needs a List of Strings. Insert the following before you set the adapter
List<String> test = new ArrayList<String>();
test.add("1");
test.add("2"):
test.add("3");
and then your adapter should look like this
final ListAdapter adapter = new MyListAdapter(Attendance.this,
R.layout.list_att, test);
I have a custom array adapter for a ListView. The goal is to have each list item have an individual picture, and that when the user clicks on the item they are taken to a web page. I have the image part working, and there are some stackoverflow answers for where to put the onItemClickListener. It seems it would go in the custom array adapter class But I can't figure out how to access the url from the list item.
Here is the code:
public class Animal
{
int image_resource_id;
String animal_type;
String wikipedia_url;
Animal(int image_resource_id, String animal_type, String wikipedia_url)
{
this.image_resource_id = image_resource_id;
this.animal_type = animal_type;
this.wikipedia_url = wikipedia_url;
}
}
class AnimalViewHolder
{
ImageView animal_image = null;
TextView animal_name = null;
String animal_url = "";
}
class CustomAnimalAdapter extends ArrayAdapter<Animal>
{
Context context;
int layoutResourceId;
Animal data[] = null;
public CustomAnimalAdapter(Context context, int layoutResourceId, Animal[] data)
{
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
AnimalViewHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new AnimalViewHolder();
holder.animal_image = (ImageView)row.findViewById(R.id.animal_image);
holder.animal_name = (TextView)row.findViewById(R.id.animal_name);
row.setTag(holder);
}
else
{
holder = (AnimalViewHolder)row.getTag();
}
Animal animal = data[position];
holder.animal_name.setText(animal.animal_type);
holder.animal_image.setImageResource(animal.image_resource_id);
return row;
}
}
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Animal[] animal_list = {
new Animal(R.drawable.lion, "Lion", "http://en.wikipedia.org/wiki/Lion"),
new Animal(R.drawable.tiger, "Tiger", "http://en.wikipedia.org/wiki/Tiger"),
new Animal(R.drawable.bear, "Bear", "http://en.wikipedia.org/wiki/Bear"),
new Animal(R.drawable.monkey, "Monkey", "http://en.wikipedia.org/wiki/Monkey"),
new Animal(R.drawable.moose, "Moose", "http://en.wikipedia.org/wiki/Moose"),
new Animal(R.drawable.shark, "Shark", "http://en.wikipedia.org/wiki/Shark")
};
CustomAnimalAdapter adapter = new CustomAnimalAdapter(this,
R.layout.row_item, animal_list);
list_view_1 = (ListView)findViewById(R.id.listView1);
list_view_1.setAdapter(adapter);
}
update your adapter getview with
row.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(animal.wikipedia_url));
context.startActivity(browserIntent);
}
});
Hi i am writing a program using custom listview with checkbox. My intention is to start a new activity when clicking a button (Add) and the new activity displays the selected values from the listview.I am getting error when i try to add the values
the error occuring line is
selectedItems.add(adapter.getItem(position));
if i comment this line then i can check the box but it shows eror when i click "Add" button.
here is my complete code.
My mainactivity
HomePage.java
public class HomePage extends Activity {
private ListView listView1;
ListAdapter adapter;
Button btn;
SparseBooleanArray checkedValue;
ArrayList<List> selectedItems;
String name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
List device_data[] = new List[]
{
new List(R.drawable.ic_pic , "Actvity1"),
new List(R.drawable.ic_pic, "Actvity2"),
new List(R.drawable.ic_pic, "Actvity3"),
new List(R.drawable.ic_pic, "Actvity4"),
new List(R.drawable.ic_pic, "Actvity5")
};
ListAdapter adapter = new ListAdapter(this, R.layout.list_viewrow, device_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
Log.i("check box status", ""+ListAdapter.ListHolder.class);
btn= (Button)findViewById(R.id.add);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] outputStrArr = new String[selectedItems.size()];
checkedValue= listView1.getCheckedItemPositions();
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i).toString();
}
Intent intent = new Intent(getApplicationContext(),
CheckedValues.class);
Bundle b = new Bundle();
b.putStringArray("selectedItems", outputStrArr);
// Add the bundle to the intent.
intent.putExtras(b);
startActivity(intent);
}
});
}
private class ListAdapter extends ArrayAdapter<List> {
Context context;
int layoutResourceId;
boolean checkvalue;
List data[] = null;
public ListAdapter(Context context, int layoutResourceId, List[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
final ListHolder holder;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ListHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.checkbox=(CheckBox) row.findViewById(R.id.check);
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
selectedItems.add(adapter.getItem(position));
}
});
row.setTag(holder);
}
else
{
holder = (ListHolder)row.getTag();
}
List list = data[position];
holder.txtTitle.setText(list.title);
holder.imgIcon.setImageResource(list.icon);
return row;
}
class ListHolder
{
ImageView imgIcon;
TextView txtTitle;
CheckBox checkbox;
}
}
}
and my second activity
CheckedValues.java
public class CheckedValues extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checked_values);
Bundle b = getIntent().getExtras();
String[] resultArr = b.getStringArray("selectedItems");
ListView lv = (ListView) findViewById(R.id.outputList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, resultArr);
lv.setAdapter(adapter);
}
}
thanks in advance..
initialize the "ArrayList"
ArrayList<List> selectedItems;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
selectedItems = new ArrayList<List>();
}
I have a ListView in one xml referencing a TextView in another xml. I'm trying to change the color of the TextView but this doesn't seem to work. If I don't set the 'setContentView' twice I got a NullPointerException.
setContentView(R.layout.text_list);
TextView textView = (TextView) findViewById(R.id.logText);
if (logLevel == "E"){
textView.setTextColor(Color.parseColor("#FF4D4D"));
}
else if (logLevel == "W"){
textView.setTextColor(Color.parseColor("#EAAB55"));
}
else if (logLevel == "I"){
textView.setTextColor(Color.parseColor("#AFD778"));
}
else if (logLevel == "V"){
textView.setTextColor(Color.parseColor("#OOOOOO"));
}
else {
textView.setTextColor(Color.parseColor("#AFD778"));
}
setContentView(R.layout.log_cat);
ListView lv1 = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this, R.layout.text_list, log);
// setContentView(parm ) for twice may cause problem. So If there need view from different layout then simply inflate them
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.log_cat);
ListView lv1 = (ListView) findViewById(R.id.listView1);
CustomAdapter adapter = new CustomAdapter(this, "V");
lv1 .setAdapter(adapter);
adapter.notifyDataSetChanged();
}
// custom adapter
public class CustomAdapter extends BaseAdapter {
private Context ctx;
private String logLevel;
CustomAdapter (ArrayList<String> data, Context context, String log)
{
this.ctx = context;
this.logLevel = log;
}
#Override
public int getCount()
{
list.size();
}
#Override
public Object getItem(int position)
{
return null;
}
#Override
public long getItemId(int position)
{
return position ;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflator = (LayoutInflater)ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.text_list, null);
TextView textView = (TextView) v.findViewById(R.id.logText);
if (logLevel == "E"){
textView.setTextColor(Color.parseColor("#FF4D4D"));
}
else if (logLevel == "W"){
textView.setTextColor(Color.parseColor("#EAAB55"));
}
else if (logLevel == "I"){
textView.setTextColor(Color.parseColor("#AFD778"));
}
else if (logLevel == "V"){
textView.setTextColor(Color.parseColor("#OOOOOO"));
}
else {
textView.setTextColor(Color.parseColor("#AFD778"));
textView.setTextColor(Color.BLUE);
}
return textView;
}
}
I have a listview, and i start an intent to fill it. But the list shows up with a default image and no text. It is supposed to show up with different images and text.
I have two arrays, one is in string type and the other one is in drawable type... But my list doesn't show me none of these that i wanted...
My ListActivity:
public class fillLeftMenu extends ListActivity {
private View menu;
ImageView findLocation;
Spinner cityList;
ListView leftList;
String [] textIndex;
Drawable [] imageIndex;
ArrayList<LeftListItems> Left;
listAdapter lAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataTransfer dt = BonubonActivity.deneme;
menu = dt.getView();
findLocation = (ImageView) menu.findViewById(R.id.image_findLocation);
leftList = (ListView) menu.findViewById(R.id.list);
// add items to listView
Left = new ArrayList<LeftListItems>();
textIndex = new String[] {"Bugünün Fırsatları", "Son Dakika Bonusları", "Kadın", "Aile", "Çocuk", "Alışveriş", "Şehirden Kaçış", "Kışa Özel"};
imageIndex = new Drawable[] {leftList.getResources().getDrawable(R.drawable.kucukicon_01),leftList.getResources().getDrawable(R.drawable.kucukicon_02),leftList.getResources().getDrawable(R.drawable.kucukicon_03),leftList.getResources().getDrawable(R.drawable.kucukicon_04),leftList.getResources().getDrawable(R.drawable.kucukicon_05),leftList.getResources().getDrawable(R.drawable.kucukicon_06),leftList.getResources().getDrawable(R.drawable.kucukicon_07),leftList.getResources().getDrawable(R.drawable.kucukicon_08)};
lAdapter = new listAdapter(menu.getContext(), R.layout.list_item, Left);
leftList.setAdapter(lAdapter);
getIndex();
lAdapter.notifyDataSetChanged();
leftList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
finish();
}
private void getIndex() {
try{
LeftListItems item = new LeftListItems();
for(int i=0; i<textIndex.length; i++) {
item.setText(textIndex[i]);
item.setImage(imageIndex[i]);
Left.add(item);
lAdapter.add(Left.get(i));
}
}
catch (Exception e) {
Log.e("BACKGROUND_PROC", e.getMessage());
}
}
private class listAdapter extends ArrayAdapter<LeftListItems> {
private ArrayList<LeftListItems> items;
private Context ctx;
public listAdapter(Context ctx, int textViewResourceId, ArrayList<LeftListItems> items) {
super(ctx, textViewResourceId, items);
this.ctx = ctx;
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
}
LeftListItems index = items.get(position);
if(index != null) {
TextView text = (TextView) v.findViewById(R.id.text);
ImageView img = (ImageView) v.findViewById(R.id.icon);
if(text != null)
text.setText(index.getText());
if(img != null)
img.setBackgroundDrawable(index.getImage());
}
return v;
}
}
}
Try calling getIndex() method before creating the adapter and setting it to the list. Like this:
getIndex();
//then create new adapter
lAdapter = new listAdapter(menu.getContext(), R.layout.list_item, Left);
leftList.setAdapter(lAdapter);