Android : CustomList view and TreeObserver

on Monday, September 1, 2014


I have two textviews in my listview. I am setting text into them from two String arrays i.e substring and substringtwo. Here I have use treeobserver to get the width of first textview in term of characters. I am displaying only single line in first textview and rest part of that string in second textview. I have successfully implemented the above functionality. But now I want a list,wherein each row consists of two rows. I have used custom adapter,but I am getting NULL POINTER EXCEPTION.Please help me how to and where to use TreeObserver in this context.



**MainActivity.java**

public class MainActivity extends Activity {


ListView lsview;
ListViewCustomAdapter adapter;
static float width;
private static String chat[]={
"Hi, How are you. I am fine.. you tell long time no seee....
blaa blaaaaaaaaaaaaaaa bla lvl","hashasahahahahjsaasasaasjasaj"};
private static String rem_chat[]={"as","asa"};
TextView tx,t2;


static String substring[],substringtwo[];




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


// setContentView(R.layout.listitem_row);
tx=(TextView) findViewById(R.id.firsttv);
t2=(TextView) findViewById(R.id.sectv);


tx.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

@SuppressLint("NewApi") @Override
public void onGlobalLayout() {
// TODO Auto-generated method stub

initalize();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
tx.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
tx.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}


}
});

lsview = (ListView) findViewById(R.id.listView3);
adapter = new ListViewCustomAdapter(this,substring,substringtwo);
lsview.setAdapter(adapter);

}



public void initalize(){

width=tx.getWidth();
System.out.println("Initailse"+width);
int totalCharstoFit;

for(int i=0;i<chat.length;i++)
{

totalCharstoFit=tx.getPaint().breakText(chat[i], 0, chat[i].length(),
true, width, null);


System.out.println(totalCharstoFit);

try{
substring[i]=chat[i].substring(0,totalCharstoFit);
substringtwo[i]=chat[i].substring(substring[i].length(),chat[i].length());


//tx.setText(substring);
// t2.setText(substringtwo);
// System.out.println(substring);


/*
lsview = (ListView) findViewById(R.id.listView3);
adapter = new ListViewCustomAdapter(this,substring,substringtwo);
lsview.setAdapter(adapter); */
}catch(Exception e){System.out.println(e);
}
}
}


}




**ListViewCustomAdapter.java**


public class ListViewCustomAdapter extends BaseAdapter
{
public String title[];
public String description[];
public Activity context;
public LayoutInflater inflater;

public ListViewCustomAdapter(Activity context,String[] title, String[] description){
super();

this.context = context;
this.title = title;
this.description = description;

this.inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE)
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return title.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

public static class ViewHolder
{

TextView txtViewTitle;
TextView txtViewDescription;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.listitem_row, null);


holder.txtViewTitle = (TextView) convertView.findViewById(R.id.firsttv);
holder.txtViewDescription = (TextView) convertView.findViewById(R.id.sectv);

convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();


holder.txtViewTitle.setText(title[position]);
holder.txtViewDescription.setText(description[position]);

return convertView;
}

}




**activity_main.xml**

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >

<ListView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/listView3"
android:layout_alignParentLeft="true">
</ListView>

</RelativeLayout>


**listitem_row.xml**

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >

<Button android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>

<TextView
android:id="@+id/firsttv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_toRightOf="@+id/btn"
android:layout_alignBottom="@+id/btn"
android:maxLines="1"
/>

<TextView
android:id="@+id/sectv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_below="@+id/btn"
android:textColor="#FF00FF" />

</RelativeLayout>

0 comments:

Post a Comment