aquaballoon

Android - BaseAdapter

Apr 27th, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.99 KB | None | 0 0
  1. package jason.listviewsample.app;
  2.  
  3. import android.support.v7.app.ActionBarActivity;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.AdapterView;
  11. import android.widget.BaseAdapter;
  12. import android.widget.ListView;
  13. import android.widget.TextView;
  14.  
  15. import java.util.ArrayList;
  16.  
  17.  
  18. public class BaseAdapterListView extends ActionBarActivity {
  19.  
  20.     ListView listView;
  21.     String listItem;
  22.     MyAdapter customAdapter;
  23.  
  24.  
  25.     @Override
  26.     protected void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.         setContentView(R.layout.activity_base_adapter_list_view);
  29.  
  30.         customAdapter = new MyAdapter();
  31.  
  32.         listView = (ListView) findViewById(R.id.listViewBaseAdapter);
  33.         listView.setAdapter(customAdapter);
  34.  
  35.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  36.             @Override
  37.             public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  38.  
  39.             }
  40.         });
  41.     }
  42.  
  43.  
  44.     @Override
  45.     public boolean onCreateOptionsMenu(Menu menu) {
  46.         // Inflate the menu; this adds items to the action bar if it is present.
  47.         getMenuInflater().inflate(R.menu.base_adapter_list_view, menu);
  48.         return true;
  49.     }
  50.  
  51.     @Override
  52.     public boolean onOptionsItemSelected(MenuItem item) {
  53.         // Handle action bar item clicks here. The action bar will
  54.         // automatically handle clicks on the Home/Up button, so long
  55.         // as you specify a parent activity in AndroidManifest.xml.
  56.         int id = item.getItemId();
  57.         if (id == R.id.action_settings) {
  58.             return true;
  59.         }
  60.         return super.onOptionsItemSelected(item);
  61.     }
  62.  
  63.     public class Data {
  64.         private String letter;
  65.         private String number;
  66.     }
  67.  
  68.     public ArrayList<Data> addData(){
  69.         ArrayList<Data> listData = new ArrayList<Data>();
  70.  
  71.         Data data1 = new Data();
  72.         data1.letter = "A";
  73.         data1.number = "1";
  74.         listData.add(data1);
  75.  
  76.         Data data2 = new Data();
  77.         data2.letter = "B";
  78.         data2.number = "2";
  79.         listData.add(data2);
  80.  
  81.         Data data3 = new Data();
  82.         data3.letter = "C";
  83.         data3.number = "3";
  84.         listData.add(data3);
  85.  
  86.         Data data4 = new Data();
  87.         data4.letter = "D";
  88.         data4.number = "4";
  89.         listData.add(data4);
  90.  
  91.         return listData;
  92.     }
  93.  
  94.     public class MyAdapter extends BaseAdapter {
  95.  
  96.         String data1[]= new String[]{"Orange", "Kiwi", "Banana", "Apple"};
  97.         String data2[]= new String[]{"100", "200", "300", "400" };
  98.  
  99.         ArrayList<Data> listData;
  100.  
  101.         LayoutInflater inflater;
  102.  
  103.         @Override
  104.         // looping
  105.         public int getCount() {
  106.             return data1.length;
  107.         }
  108.  
  109.         @Override
  110.         public Object getItem(int index) {
  111.             return null;
  112.         }
  113.  
  114.         @Override
  115.         public long getItemId(int index) {
  116.             return index;
  117.         }
  118.  
  119.         @Override
  120.         public View getView(int index, View view, ViewGroup viewGroup) {
  121.  
  122.             inflater = getLayoutInflater();
  123.             //    LayoutInflater inflater;
  124.             //    inflater = (LayoutInflater) BaseAdapterListView.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  125.             view = inflater.inflate(R.layout.list_view_design, viewGroup, false);
  126.  
  127.             TextView textView1 = (TextView)view.findViewById(R.id.textView1);
  128.             TextView textView2 = (TextView)view.findViewById(R.id.textView2);
  129.  
  130.             textView1.setText(data1[index]);
  131.             textView2.setText(data2[index]);
  132.  
  133.             listData = addData();
  134.             Data data = listData.get(index);
  135.  
  136. //            textView1.setText(data.letter);
  137. //            textView2.setText(data.number);
  138.  
  139.             return view;
  140.         }
  141.     }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment