Mehmet Ethem SULAN :: Android :: Android Create Customize Adapter and ListView Items

Android ile Twitter‘a benzer bir basit uygulama yazmaya çalıştım. Normalde uygulamaya login olurken username, profil image ve password sqlite veritabanında tutuluyor ve değerler oradan alınıyor ama örnek sade olsun diye o tarafa girmedim. Bu örneği daha iyi anlamak için Android Custom ListView Items and Adaptera bakabilirsiniz.

Uygulama image, user_name ve text alanalrından oluşuyor. En son girilen input en ListView‘de en üste ekleniyor.

BlipListActivity.java

package com.blip.list;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class BlipListActivity extends Activity {
    private ListView listView;
    private Button button_send_blip;
    private EditText editText_input_text;
    private BlipVar blipVar_obj;
    private BlipAdapter blipAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        listView=(ListView) findViewById(R.id.listView1_blip);
        button_send_blip=(Button) findViewById(R.id.button1_send);
        editText_input_text=(EditText) findViewById(R.id.editText1_input);
        Conf.arrayList.clear();
        
//        BlipVar object=new BlipVar();
//        object.setUser_name("mehmet");
//        object.setImage("http://www.ethemsulan.com/wp-content/themes/mahinhin/images/kelime.png");
//        object.setText("Merhaba bu bir textttir");
//		Conf.arrayList.add(object);
//Kendi adapterimizi yazdik ve listview ile ilişkilendirdik.
        blipAdapter=new BlipAdapter(this, Conf.arrayList);
        listView.setAdapter(blipAdapter);
        
        
        button_send_blip.setOnClickListener(send_blip);
    }
    
    OnClickListener send_blip=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			blipVar_obj=new BlipVar();
			blipVar_obj.setUser_name("isil");
			blipVar_obj.setImage("http://www.ethemsulan.com/wp-content/themes/mahinhin/images/kelime.png");
			blipVar_obj.setText(editText_input_text.getText().toString());
			Conf.arrayList.add(0, blipVar_obj);
//Adaptarimiza data girildiğinde kendisini güncelliyor ve girilen data hemen alta eklenir.
//Yani UI güncelleniyor.
			blipAdapter.notifyDataSetChanged();
			editText_input_text.setText("");
			
		}
	};
}

BlipAdapter.java

package com.blip.list;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class BlipAdapter extends ArrayAdapter<BlipVar>{
//BlipVar nesne listesi "names" ekleniliyor.
	private final Activity context;
	private ArrayList<BlipVar> names;

	public BlipAdapter(Activity context,ArrayList<BlipVar> names) {
//Bu username, textview ve image için layouyt oluşrurup nasıl gösterileceğine karar veriyoruz
//R.layout.blip_adapter
		
		super(context, R.layout.blip_adapter, names);
		this.context = context;
		
		this.names = names;
	}

	static class ViewHolder {
		public ImageView imageView;
		public TextView textView_user_name,textView_body_text;
	}

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

		final ViewHolder holder;
		
		View rowView = convertView;
		if (rowView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.blip_adapter, null, true);
			holder = new ViewHolder();
			holder.textView_user_name = (TextView) rowView.findViewById(R.id.textView1_user_name);
			holder.imageView = (ImageView) rowView.findViewById(R.id.imageView1);
			holder.textView_body_text=(TextView) rowView.findViewById(R.id.textView1_blip_text);
			rowView.setTag(holder);
		} else {
			holder = (ViewHolder) rowView.getTag();
		}
//Gelen obje içindeki dataları set ediyor.
		BlipVar blipVar=names.get(position);
		
		if(blipVar!=null)
		{
			holder.textView_user_name.setText(blipVar.getUser_name());
			holder.imageView.setImageDrawable(Conf.loadImageFromURL(blipVar.getImage()));
			
			holder.textView_body_text.setText(blipVar.getText());
		}		
		
		return rowView;
	}

}

BlipVar.java

package com.blip.list;

public class BlipVar {
//Adapterimizd ehang, alanalr varsa o alanalr tanimlanir.
	private String user_name,image,text;

	public String getUser_name() {
		return user_name;
	}

	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}
	
}

Conf.java


package com.blip.list;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.util.Log;

public class Conf {

	public static ArrayList<BlipVar> arrayList=new ArrayList<BlipVar>();
	
	//Internetten image cekiyor
	 public static Drawable loadImageFromURL(String url)
	    {
	    try
	    {
	    InputStream is = (InputStream) new URL(url).getContent();
	    Drawable d = Drawable.createFromStream(is, "src");
	    return d;
	    }catch (Exception e) {
	    System.out.println(e);
	    return null;
	    }
	 }
	
	private static InputStream OpenHttpConnection(String urlString) throws IOException
    {
		//Log.d(TAG, "InputStream");	
		InputStream in = null;
		while(in==null){
			int response = -1;
               
			URL url = new URL(urlString);
			URLConnection conn = url.openConnection();
        
			if (!(conn instanceof HttpURLConnection)) throw new IOException("Not an HTTP connection");
        	
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.setReadTimeout(5000);
            httpConn.setConnectTimeout(5000);
            httpConn.connect();        
            response = httpConn.getResponseCode();                 
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();                                 
            }
        }
        return in;     
    }
	
	

}

blip_adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:orientation="vertical">
    <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:weightSum="1" android:baselineAligned="true">
        <ImageView android:src="@drawable/icon" android:id="@+id/imageView1" android:layout_height="50dip" android:layout_width="60dip" android:layout_marginLeft="10dip"></ImageView>
        <TextView android:text="TextView" android:layout_width="wrap_content" android:id="@+id/textView1_user_name" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="fill_parent" android:layout_marginLeft="50dip"></TextView>
    </LinearLayout>
    <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout2" android:layout_height="wrap_content" android:orientation="vertical">
        <TextView android:text="TextView" android:layout_width="wrap_content" android:id="@+id/textView1_blip_text" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_gravity="center"></TextView>
    </LinearLayout>
    
</LinearLayout>

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"
    >
    <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content">
        <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/editText1_input" android:inputType="textMultiLine">
            <requestFocus></requestFocus>
        </EditText>
    </LinearLayout>
    <Button android:layout_gravity="right" android:text="@string/send_blip" android:id="@+id/button1_send" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <ListView android:id="@+id/listView1_blip" android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView>
</LinearLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, BlipListActivity!</string>
    <string name="app_name">BlipList</string>
    <string name="send_blip">Send New Blip</string>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.blip.list"
      android:versionCode="1"
      android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".BlipListActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Download BlipList.tar.gz

Write a Comment

Let me know what you think?