Posted by Shoaib at 12:09:00 am
Read our previous post
MainActivity.java
package com.example.shoaibm.teachers; import android.app.Activity; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { ListView listview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final String[] data = new String[]{"Java","Python", "C++"}; int[] i = {R.drawable.cake, R.drawable.carrot, R.drawable.fruit}; listview = (ListView)findViewById(R.id.listView); final MyAdapter adapter = new MyAdapter(this,data,i); listview.setAdapter(adapter); listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // Toast.makeText(getApplicationContext(),"clicked on: "+data[position], Toast.LENGTH_LONG).show(); Toast.makeText(getApplicationContext(),"clicked on: "+adapter.getItem(position), Toast.LENGTH_LONG).show(); } }); } @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_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
MyAdapter.java:
package com.example.shoaibm.teachers; import android.app.Activity; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; /** * Created by Shoaib M on 5/12/2015. */ public class MyAdapter extends BaseAdapter { String[] i; Context ctx; int[] img; public MyAdapter(Context ctx, String[] i,int[] img) { super(); this.i=i; this.ctx = ctx; this.img = img; } @Override public int getCount() { return i.length; } @Override public Object getItem(int position) { return i[position]; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = LayoutInflater.from(ctx).inflate(R.layout.my_list, parent,false); TextView tv = (TextView)convertView.findViewById(R.id.txt); ImageView iv = (ImageView)convertView.findViewById(R.id.imageView); tv.setText(i[position]); iv.setImageResource(img[position]); return convertView; } }
layout\activity_main.xml:
//schemas.android.com/apk/res/android"
Layout\my_list.xml:
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">//schemas.android.com/apk/res/android"
No comments:
Post a Comment