PowenKo, Android, Tutorial 032-4, ListView, Custom ListView items and adapters extends Activity

August 22, 2011   ·   0 Comments

News.java

package com.powenko.Tutorial_Listview_CustomListViewItemsAndAdaptersExtendsActivity;

public class News
{
	public String _title="";
	public String _link="";

  public String getTitle()
  {
    return _title;
  }
  public String getLink()
  {
    return _link;
  }

}

MyAdapter

package com.powenko.Tutorial_Listview_CustomListViewItemsAndAdaptersExtendsActivity;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter
{

  public LayoutInflater mInflater;
  public List<News> items;

  public MyAdapter(Context context,List<News> it)
  {
    mInflater = LayoutInflater.from(context);
    items = it;
  }

 // @Override
  public int getCount()
  {
    return items.size();
  }

 // @Override
  public Object getItem(int position)
  {
    return items.get(position);
  }

 // @Override
  public long getItemId(int position)
  {
    return position;
  }

 // @Override
  public View getView(int position,View convertView,ViewGroup par)
  {

   String a1="";
    if(convertView == null)
    {
    	 convertView = mInflater.inflate(R.layout.row, null);

    	    News tmpN=(News)items.get(position);

    	    String tgetcategory=tmpN.getcategory();

    	      convertView.setTag(holder);

    }

    return convertView;
  }

}
package com.powenko.Tutorial_Listview_CustomListViewItemsAndAdaptersExtendsActivity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Tutorial_Listview_CustomListViewItemsAndAdaptersExtendsActivityActivity extends Activity {
    /** Called when the activity is first created. */
	  private List<News> li=new ArrayList<News>();
	  private static final String[] array =
	  { "1", "2", "3"};
	  LinearLayout myLinearLayout;
	  TextView myTextView;
	  ListView myListView;
	  String m_kind;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

			    News news=new News();
			    news.setcategory("1");
			    li.add(news);
			    News news2=new News();
			    news2.setcategory("2");
			    li.add(news2);
			    News news3=new News();
			    news3.setcategory("3");
			    li.add(news3);

	    myListView=(ListView) findViewById(R.id.listView1);
	    myListView.setCacheColorHint(0);
	    myListView.setAdapter(new MyAdapter(this,li));

	    myListView
	        .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
	        {

	         @Override
	          public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
	              long arg3)
	          {

	          }

	         @Override
	          public void onNothingSelected(AdapterView<?> arg0)
	          {

	          }

	        });

	    /* myListView�[�JOnItemClickListener */
	    myListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
	    {

	    //  @Override
	      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
	          long arg3)
	      {
			  Toast.makeText(
					  Tutorial_Listview_CustomListViewItemsAndAdaptersExtendsActivityActivity.this,"Selected",
	                          Toast.LENGTH_SHORT )
	              .show();

	      }
	    });

	  }

	}

res\layout\main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
   <ListView android:id="@+id/listView1" android:layout_height="wrap_content" android:layout_width="fill_parent"></ListView>

</LinearLayout>

res\layout\row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="120px"
>
<ImageView android:id="@+id/icon"
    android:layout_width="32dip"
    android:layout_height="32dip"
    android:src="@drawable/icon"
 ></ImageView>
<TextView
	android:text="TextView"
	android:id="@+id/textView1"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
</TextView>
</LinearLayout>

sample code:

Tutorial_Listview_CustomListViewItemsAndAdaptersExtendsActivity

shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Android7

Amazon

adsbygoogle

Recent Posts

Categories

Amazon