-4

I want to fields of arrayList to listView but how can I write adapter I dont know. Please help me!!!

I need a my adapter class . I have to see on listView tutar:1.34 kalem="xx"

Class:

public class Income {





String kalem;
int tutar;

public int getTutar() {
    return tutar;
}
public void setTutar(int tutar) {
    this.tutar = tutar;
}
int id;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}

public String getKalem() {
    return kalem;
}
public void setKalem(String kalem) {
    this.kalem = kalem;
}

}

ArrayList:

sqliteHelper helper = new sqliteHelper(getApplicationContext());
    ArrayList<Income> liste=helper.getAllIncome();

2 Answers2

1

There are two ways you can build the adapter:--

  • Use some adapter provided by Android system like:-
     SimpleCursorAdapter
     ArrayAdapter,etc
    

    example:--

     ListView listView = (ListView) findViewById(R.id.<list_view_id>);
     ArrayList<Income> income_array_list = <get the arraylist from whatever sources>;
     ArrayAdapter<Income> arrayAdapter = new ArrayAdapter<Income>(
             this, 
             android.R.layout.simple_list_item_1,
             income_array_list );
    
     listView.setAdapter(arrayAdapter); 
    

  • Create your own custom adapter.

    I just used only two field just to show you how to build custom adapter,Rest you can build by looking this code.

    Example:-

    IncomeListAdapter class

    private class IncomeListAdapter extends BaseAdapter {
    
        ArrayList<Income> mDisplayedValues;
        LayoutInflater inflater;
    
        public IncomeListAdapter(Context context) {
            // TODO Auto-generated constructor stub
            mDisplayedValues = <your_Income_list>;
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mDisplayedValues.size();
        }
    
        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }
    
        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub
    
            View v = convertView;
            ViewHolder holder;
            if (v == null) {
                v = inflater.inflate(R.layout.listrow_income, null);
                holder = new ViewHolder();
                holder.tvTutar = (TextView) v
                        .findViewById(R.id.tvTutar);
                holder.tvId = (TextView) v.findViewById(R.id.tvId);
    
                v.setTag(holder);
            } else
                holder = (ViewHolder) v.getTag();
                   holder.tvTutar.setText(mDisplayedValues.get(position).getTutar());
               holder.tvId.setText(mDisplayedValues.get(position).getId());
               return v;
        }
    
        class ViewHolder {
            TextView tvTutar;
            TextView tvId;
        }
    }
    

    listrow_income.xml:--

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="8dp" >
       <TextView
        android:id="@+id/tvTutar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#323232" />
       <TextView
        android:id="@+id/tvId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"    />
    
     </LinearLayout>
    
  • Kailash Dabhi
    • 3,473
    • 1
    • 29
    • 47
    0

    Try this

    private ListView lv;
    
    public void onCreate(Bundle saveInstanceState) {
         setContentView(R.layout.your_layout);
    
         lv = (ListView) findViewById(R.id.your_list_view_id);
    
         // Instanciating an array list (you don't need to do this, 
         // you already have yours).
         sqliteHelper helper = new sqliteHelper(getApplicationContext());
         ArrayList<Income> your_array_list = helper.getAllIncome();
    
         // This is the array adapter, it takes the context of the activity as a 
         // first parameter, the type of list view as a second parameter and your 
         // array as a third parameter.
         ArrayAdapter<Income> arrayAdapter = new ArrayAdapter<Income>(
                 this, 
                 android.R.layout.simple_list_item_1,
                 your_array_list );
    
         lv.setAdapter(arrayAdapter); 
    }
    
    Tabrej Khan
    • 894
    • 6
    • 15