Custom SimpleCursorAdapter not displaying data on screen - android

I've read and tried a dozen solutions from this forum but none of them seem to work - I cannot display data on my lists - view.findViewById(resourceid) returns null each time in bindView - for example when using TextView t = (TextView) view.findViewById(R.id.leftCell). It is strange behaviour considering i have no errors in Logcat, and the view itself isn't null. What am I doing wrong?
I've divided my layout as follows: two LinearLayouts each wrapping a ListView. I've defined a custom row structure for both lists, found in database_item.xml and list_item.xml.
Here's my code.
Main.xml:
<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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="#+id/topFrame">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Devices loaded from Database: "></TextView>
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView android:text="Name"
android:padding="3dip"
android:gravity="center"/>
<TextView android:text="Remote Address"
android:padding="3dip"
android:gravity="center"/>
<TextView android:text="Type"
android:padding="3dip"
android:gravity="center"/>
</TableRow>
</TableLayout>
<ListView android:id="#+id/databaseList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
</ListView>
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.2"
android:id="#+id/bottomFrame">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Detected Xbee nodes via 'introduce' command:"></TextView>
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView android:text="Address"
android:padding="3dip"
android:gravity="center"/>
<TextView android:text="Type"
android:padding="3dip"
android:gravity="center"/>
</TableRow>
</TableLayout>
<ListView android:id="#+id/detectedNodesList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
</LinearLayout>
database_item.xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:id="#+id/checkBox"></CheckBox>
<TextView android:id="#+id/topLeftCell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:gravity="center"></TextView>
<TextView android:id="#+id/topCenterCell"
android:padding="3dip"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="#+id/topRightCell"
android:gravity="center"
android:padding="3dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</TableRow>
</TableLayout>
list_item.xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow>
<TextView android:id="#+id/leftCell"
android:padding="3dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="#+id/rightCell"
android:gravity="left"
android:padding="3dip"
android:text="TESTING"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</TableRow>
Relevant code from onCreate:
ListView topList = (ListView) findViewById(R.id.databaseList);
ListView bottomList = (ListView) findViewById(R.id.detectedNodesList);
String[] topColumns = new String[] {"name", "my", "type"};
String[] bottomColumns = new String[] {"my","type"};
int[] toTop = new int[] { R.id.topLeftCell, R.id.topCenterCell, R.id.topRightCell};
int[] toBottom = new int[] { R.id.leftCell, R.id.rightCell};
DbCursorAdapter e = new DbCursorAdapter(this, R.layout.database_item, cursor, topColumns, toTop);
DbCursorAdapter d = new DbCursorAdapter(this, R.layout.list_item, cursor, bottomColumns, toBottom);
topList.setAdapter(e);
bottomList.setAdapter(d);
and DbCursorAdapter.java:
public class DbCursorAdapter extends SimpleCursorAdapter {
private Context context;
private int layout;
private final String TAG = "DbCursorAdapter";
LayoutInflater inflater;
public DbCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
super(context,layout,c,from,to);
this.context = context;
this.layout = layout;
inflater = LayoutInflater.from(context);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflater.inflate(layout, parent, false);
return v;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
Cursor c = cursor;
if (layout == R.layout.list_item) {
int addressCol = c.getColumnIndex("my");
int typeCol = c.getColumnIndex("type");
int address = c.getInt(addressCol);
int type = c.getInt(typeCol);
TextView t = (TextView) view.findViewById(R.id.leftCell);
TextView t2 = (TextView) view.findViewById(R.id.rightCell);
if (t != null) {
t.setText(Integer.toString(address));
}
if (t2 != null) {
t2.setText(Integer.toString(type));
}
}
if (layout == R.layout.database_item) {
int nameCol = c.getColumnIndex("name");
int addressCol = c.getColumnIndex("my");
int typeCol = c.getColumnIndex("type");
String name = c.getString(nameCol);
int my = c.getInt(addressCol);
int type = c.getInt(typeCol);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox);
checkBox.setVisibility(CheckBox.VISIBLE);
TextView t = (TextView) view.findViewById(R.id.topLeftCell);
TextView t2 = (TextView) view.findViewById(R.id.topCenterCell);
TextView t3 = (TextView) view.findViewById(R.id.topRightCell);
if (t != null) {
t.setText(name);
}
if (t2 != null) {
t2.setText(Integer.toString(my));
}
if (t3 != null) {
t3.setText(Integer.toString(type));
}
}
}

if (layout == R.layout.list_item)
The right way to access a resource id is
int list_item_id = context.getResources.getIdentifier(list_item,"layout", getPackageName());
if (layout == list_item_id)
this will return you the resource id of R.layout.list_item . I wanted to just let you know, but i don't know if this is the exact problem.

Much like Yashwanth, my intuition is telling me something is up with if(layout==R.layout.list_item). Have you considered just using separate adapters for each list?

Solved. The problem seemed to be in main.xml:
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView android:text="Name"
android:padding="3dip"
android:gravity="center"/>
<TextView android:text="Remote Address"
android:padding="3dip"
android:gravity="center"/>
<TextView android:text="Type"
android:padding="3dip"
android:gravity="center"/>
</TableRow>
</TableLayout>
Somehow covered the ListView that was supposed to be below it for both the top frame and the bottom frame. Removing TableLayout shows both lists just fine.
Would anybody be able to explain why?

Related

Android's simple cursor adapter and multiple row layouts

I've been dabbling with android here and there for the past few months (mostly just tutorials). I've been working on a texting app (for myself) in my free time to learn, and I've succeeded in getting a different layout for each row, but it doesn't work as I'd intended.
Each row is either going to be left or right (left by default), depending on if the number that's in the cursor's position is my phone number or not.
The problem is that my test data looks like this (where the green is my text):
Here's the code for my custom adapter:
public class ConversationMessageListAdapter extends SimpleCursorAdapter {
private LayoutInflater inflater;
private String myPhoneNumber;
public ConversationMessageListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to, int flags) {
super(context, layout, cursor, from, to, flags);
myPhoneNumber = getMyPhoneNumber(context);
inflater = LayoutInflater.from(context);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
String number = cursor.getString(2);
System.out.println("phone#: "+number);
System.out.println("my#: "+myPhoneNumber);
if(number.equals(myPhoneNumber)){
View view = inflater.inflate(R.layout.single_message_layout_right, null);
return view;
}
return super.newView(context, cursor, parent);
}
public String getMyPhoneNumber(Context context){
TelephonyManager tMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return tMgr.getLine1Number();
}
}
You can see my attempt at debugging the issue by printing out the values of myPhoneNumber and number. It looks like they aren't always matching up correctly with the rows. It'll show the friend's number on the row where it should be mine, yet if I leave the default layout (keep them all to the left) it shows my number on that same row in the textview. Like:
555-555-FRND
Have you played Dynasty Warriors?
555-555-MINE
Not yet. Maybe soon.
But at that point, what get's printed is:
phone#: 555-555-FRND
my# 555-555-MINE
phone#: 555-555-FRND
my# 555-555-MINE
When it should look like:
phone#: 555-555-FRND
my# 555-555-MINE
phone#: 555-555-MINE
my# 555-555-MINE
I'm at a loss for what's causing this.
If it helps, here's my setViewBinder:
textsDbAdapter.setViewBinder(new ConversationMessageListAdapter.ViewBinder() {
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(columnIndex == 2){
String number = cursor.getString(columnIndex);
String sentBy = getContactName(contentResolver, number);
if(number.equals(myPhoneNumber)){
return false;
}
TextView sender = (TextView) view;
sender.setText(sentBy);
return true;
}
//datetime
if(columnIndex == 5){
Long messageDatetime = Long.valueOf(cursor.getString(columnIndex));
String receivedOn;
if(startOfToday > messageDatetime){
receivedOn = getDateFromDatetime(messageDatetime);
} else {
receivedOn = getTimeFromDatetime(messageDatetime);
}
TextView datetime = (TextView) view;
datetime.setText(receivedOn);
return true;
}
return false;
}
});
Edit: I've also found out that when I scroll up and then back down, the rows will align randomly.
EDIT2: XML added
single_message_layout_right.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"
android:paddingLeft="0dp"
android:paddingRight="5dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:layout_gravity="right"
tools:context=".ConversationActivity">
<!--Datetime Received-->
<TextView
android:id="#+id/datetime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_below="#+id/messageContent"
android:layout_toLeftOf="#+id/displayPicContainer"
android:textSize="14sp"
android:textColor="#9110828f" />
<!--Message-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/messageContent"
android:textSize="15sp"
android:layout_marginTop="5dp"
android:layout_toLeftOf="#+id/displayPicContainer"/>
<!--Display picture layout-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="3dp"
android:layout_marginLeft="5dp"
android:id="#+id/displayPicContainer">
<!--Display picture-->
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:src="#drawable/bigboss150x150"
android:id="#+id/displayPic"/>
</LinearLayout>
</RelativeLayout>
single_message_layout_left.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"
android:paddingLeft="0dp"
android:paddingRight="5dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
tools:context=".ConversationActivity">
<!--Display picture layout-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:layout_marginRight="5dp"
android:id="#+id/displayPicContainer">
<!--Display picture-->
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:src="#drawable/bigboss150x150"
android:id="#+id/displayPic"/>
</LinearLayout>
<!-- sender -->
<TextView
android:id="#+id/sender"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="NAME"
android:textColor="#ff9b9aa0"
android:layout_toRightOf="#+id/displayPicContainer"/>
<!--Datetime Received-->
<TextView
android:id="#+id/datetime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_below="#+id/messageContent"
android:layout_toRightOf="#+id/displayPicContainer"
android:textSize="14sp"
android:textColor="#9110828f" />
<!--Message-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/messageContent"
android:textSize="15sp"
android:layout_below="#+id/sender"
android:layout_toRightOf="#+id/displayPicContainer"/>
</RelativeLayout>
conversation_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/conversationListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="43dp"
android:stackFromBottom="true"
android:transcriptMode="normal">
</ListView>
<RelativeLayout
android:id="#+id/form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:background="#ffffff">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:inputType="textCapSentences|textMultiLine"
android:ems="10"
android:lines="5"
android:minLines="1"
android:hint="Enter message"
android:id="#+id/newMessageText"
android:scrollHorizontally="true"
android:scrollbars="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/sendButton"/>
<Button
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="Send"
android:id="#+id/sendButton"
android:layout_alignBottom="#+id/newMessageText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</RelativeLayout>
I switched from doing newView to getView and this is working for me.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
Log.d("RowID", messages.getString(messages.getColumnIndex("_id")));
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
String fromNumber = messages.getString(messages.getColumnIndex("fromNumber"));
if(fromNumber.equals(myPhoneNumber)){
row = inflater.inflate(R.layout.single_message_layout_right, parent, false);
} else {
row = inflater.inflate(R.layout.single_message_layout_left, parent, false);
TextView sender = (TextView) row.findViewById(R.id.sender);
String name = getContactName(fromNumber);
sender.setText(name);
}
TextView datetime = (TextView) row.findViewById(R.id.datetime);
Long messageDatetime = messages.getLong(messages.getColumnIndex("datetime"));
String receivedOn;
if(startOfToday > messageDatetime){
receivedOn = getDateFromDatetime(messageDatetime);
} else {
receivedOn = getTimeFromDatetime(messageDatetime);
}
datetime.setText(receivedOn);
String message = messages.getString(messages.getColumnIndex("message"));
TextView messageContent = (TextView) row.findViewById(R.id.messageContent);
messageContent.setText(message);
return row;
}

How to get two textviews in a listview on the same line?

I have two TextViews tvA and tvB which appear in a ListView lvA.
tvA and tvB display data pulled from a Sqlite Database using Cursor.
Cursor cursor = myDb.getAllRows();
String[] from = new String[]
{DBAdapter.KEY_A,DBAdapter.KEY_B};
int[] to = new int[]
{R.id.tvA,R.id.tvB};
SimpleCursorAdapter myCursorAdapter =new SimpleCursorAdapter(this,R.layout.lvA, cursor,from,to);
myList = (ListView) findViewById(R.id.lvA);
myList.setAdapter(myCursorAdapter);
registerForContextMenu(myList);
The issue is that tvA and tvB doesn't appear as one item in lvA (i.e. they don't appear on the same line) and appear as two different items.
Below is the XML:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="5dip"
>
<TextView
android:id="#+id/tvA"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"/>
<TextView
android:id="#+id/tvB"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_toRightOf="#id/tvA"/>
</RelativeLayout>
If you want both the TextViews in the same align then you can use LinearLayout with android:layout_weight attribute.
To have equal width TextView, give same weight value.
You can use TableLayout
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableRow
android:id="#+id/tableRow1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView android:id="#+id/tvA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</TextView>
<TextView android:id="#+id/tvB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</TextView>
</TableRow></TableLayout >
or LinearLayout with android:orientation="horizontal"
and TextView with android:layout_weight="1"
Here's how I do it with three items on the same row = This is my item list filled from a cursor
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/TextView_StudentName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:maxWidth="75dp"
android:layout_gravity="left"
android:text="#string/student_id"
android:padding="5dp"></TextView>
<TextView
android:id="#+id/TextView_StudentLocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:maxWidth="75dp"
android:gravity="center"
android:text="#string/student_Location"
android:padding="5dp"></TextView>
<TextView
android:id="#+id/TextView_StudentClass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:maxWidth="75dp"
android:layout_alignParentRight="true"
android:text="#string/student_class" >
</TextView>
</RelativeLayout>`
I'm in the process of replacing the hard sizes with info from the dimen file, but so far this has worked.
TRY this :
Main Java:
//DECLARE:
private Map<String, Map<String, String>> mMenuView = new HashMap<String, Map<String, String>>();
//IN onCreateView
Adapter mAdapter = new NewAdapter(getActivity().getApplicationContext(), mMenuView);
mListView.setAdapter(mAdapter);
// ADD onActivityCreated or onStart save data from your DB and store in mMenuView
Map<String, String> tempMap = new HashMap<String, String>();
tempMap.put("TEXTVALE_A", datafromdb_A);
tempMap.put("TEXTVALE_B", datafromdb_B);
mMenuView.put(Integer.toString(mMenuView.size()), tempMap);
}
//NEWADAPTER
private class NewAdapter extends BaseAdapter {
private Context mContext;
private TextView mTitle1;
private TextView mTitle2;
private Map<String, Map<String, String>> mData = new HashMap<String, Map<String, String>>();
public NewAdapter(Context applicationContext,Map<String, Map<String, String>> mMenuValue) {
mContext = applicationContext;
mData = mMenuValue;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public Map<String, String> getItem(int position) {
return mData.get(Integer.toString(position));
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Map<String, String> data = mData.get(Integer.toString(position));
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.linear_layout, null);
mTitle1 = (TextView) convertView.findViewById(R.id.t1A);
mTitle2 = (TextView) convertView.findViewById(R.id.t1B);
}
if (data != null) {
mTitle1.setText(data.get("TEXTVALE_A"));
mTitle2.setText(data.get("TEXTVALE_B"));
}
return convertView;
}
}
linear_layout.xml
<LinearLayout
android:id="#+id/content_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:id="#+id/tab1_wrapper"
android:layout_width="0dip"
android:layout_height="50dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_gravity="center|center_horizontal"
>
<TextView
android:id="#+id/t1A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="13sp"
android:gravity="center|center_horizontal"
android:textColor="#color/text_color3x"/>
</LinearLayout>
<LinearLayout
android:id="#+id/tab2_wrapper"
android:layout_width="0dip"
android:layout_height="50dp"
android:layout_weight="0.5"
android:orientation="vertical"
android:gravity="left|center_vertical|center_horizontal"
>
<TextView
android:id="#+id/A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="13sp"
android:gravity="center|center_horizontal"
/>
</LinearLayout>
</LinearLayout>

Is there a way to display TableLayout in ListView

I have 10+ tables created in xml Tablelayout and i'm trying to
put them in ListView rather than in a ScrollView .
Tables are not editable and just for info... How to put them as
data source in Adapter or i need to Inflate or what? tnx and sorry for noobish q..
You need to create custom Adapter for the ListView(please search this). With the adapter you need to create ListView item layout, where you can define your table for each row in the List View.
Hope this helps. :)
You can do it by simply adding TableLayout to ListView row layout xml.
Example from my current application:
public class DistributorsListAdapter extends ArrayAdapter<String>{
private final Context context;
private final int rowResourceId;
private final JSONArray data;
public DistributorsListAdapter(Context context, int rowResourceId, String[] ids, JSONArray data) {
super(context, rowResourceId, ids);
this.context = context;
this.rowResourceId = rowResourceId;
this.data = data;
}
#Override
public View getView(int position, View v, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(rowResourceId, parent, false);
try {
JSONObject rowData = data.getJSONObject(position);
// Заполняем данными строку списка
setHTMLData(rowView, rowData, "name", R.id.distributor_table_title );
setHTMLData(rowView, rowData, "address", R.id.distributor_address );
setData(rowView, rowData, "website", R.id.distributor_website );
setData(rowView, rowData, "phone", R.id.distributor_phone );
setData(rowView, rowData, "fax", R.id.distributor_fax );
setData(rowView, rowData, "email", R.id.distributor_email );
} catch (JSONException e) {
e.printStackTrace();
}
return rowView;
}
private void setData(View v, JSONObject data, String key, int textViewId) throws JSONException{
TextView tv = (TextView) v.findViewById(textViewId);
tv.setText(data.getString(key));
}
private void setHTMLData(View v, JSONObject data, String key, int textViewId) throws JSONException{
TextView tv = (TextView) v.findViewById(textViewId);
tv.setText( Html.fromHtml( data.getString(key) ) );
}
}
XML layout of a ListView row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_padding"
android:paddingRight="#dimen/activity_horizontal_padding"
android:paddingBottom="#dimen/distributor_table_vertical_padding">
<TextView
android:id="#+id/distributor_table_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/distributor_table_vertical_padding"
android:gravity="center_horizontal"
android:textSize="#dimen/distributor_table_title_text_size"
android:textStyle="bold"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/distributor_address"
android:textSize="#dimen/distributor_table_text_size"/>
<TextView
android:id="#+id/distributor_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/distributor_table_text_size"
android:layout_marginLeft="#dimen/distributor_table_columns_margin"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/distributor_website"
android:textSize="#dimen/distributor_table_text_size"/>
<TextView
android:id="#+id/distributor_website"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/distributor_table_text_size"
android:layout_marginLeft="#dimen/distributor_table_columns_margin"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/distributor_phone"
android:textSize="#dimen/distributor_table_text_size"/>
<TextView
android:id="#+id/distributor_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/distributor_table_text_size"
android:layout_marginLeft="#dimen/distributor_table_columns_margin"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/distributor_fax"
android:textSize="#dimen/distributor_table_text_size"/>
<TextView
android:id="#+id/distributor_fax"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/distributor_table_text_size"
android:layout_marginLeft="#dimen/distributor_table_columns_margin"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/distributor_email"
android:textSize="#dimen/distributor_table_text_size"/>
<TextView
android:id="#+id/distributor_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/distributor_table_text_size"
android:layout_marginLeft="#dimen/distributor_table_columns_margin"/>
</TableRow>
</TableLayout>
</LinearLayout>

Some parts of TextViews don't show and are cut off by the edge of the screen

I have a query results from a database and corresponding variables are filled.
I think that the problem is the layout, I am using incorrect layouts in my XML file. Maybe it is the ArrayAdapter, I don't know. I've spent a lot of hours trying to solve this problem and haven't been able to. Thanks for your help!
This is my XML:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView android:id="#+id/precio_offers_charger"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textStyle="bold"
android:textSize="20px"
android:textColor="#FFFFFF"
android:background="#FF0000" />
<TextView android:id="#+id/cabecera_offers_charger"
android:paddingLeft="10px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18px"
android:textColor="#FFFFFF"
android:background="#010201" />
</TableRow>
<TableRow>
<ImageView android:id="#+id/imagen"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#+id/descripcion_offers_charger"
android:paddingLeft="10px"
android:paddingTop="10px"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textStyle="normal"
android:textSize="14px" />
</TableRow>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</TableLayout>
This is a part of my class:
public class OffersChargerActivity extends ListActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.offers_charger);
...
public class AdaptadorTitulares extends ArrayAdapter<Oferta> {
Activity context;
AdaptadorTitulares(Activity context) {
super(context, R.layout.offers_charger, ofertas);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = linflater.inflate(R.layout.offers_charger, null);
}
// poblamos la lista de elementos
TextView precio = (TextView)view.findViewById(R.id.precio_offers_charger);
precio.setText(ofertas.get(position).getPrecio().concat("€"));
ImageView imagen = (ImageView) view.findViewById(R.id.imagen);
imagen.setImageBitmap(new GetImages().downloadFile(ofertas.get(position).getImagen()));
TextView cabecera = (TextView)view.findViewById(R.id.cabecera_offers_charger);
cabecera.setText(ofertas.get(position).getCabecera());
TextView descripcion = (TextView)view.findViewById(R.id.descripcion_offers_charger);
descripcion.setText(ofertas.get(position).getDescripcion());
return view;
}
}
...
}
This worked for me:
<TextView
android:id="#+id/nameText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="#android:color/black" />
Try adding in 'layout_weight' and change your 'layout_width' to "fill_parent".
Hope this helps!
don't wrap content, use fill_parent for width, and also set android:ellipsize="true"

Android : Listview of text and Image and the weird scrolling behaviour

In my application I need to create two lists each with different data . I have designed the UI like adding two listviews one after another in a TableLayout within a LinearLayout. I have stored the data in two String arrays and used arrayadapter to populate into the listview.My listview contain a textview and an image.The problem is
1.There is no error shown in the code but the list is getting populated in weird manner. 1st list item is shown, 2nd,3rd,4th list item not shown and 5th is shown 6th not shown, from 7th its fine. When i scroll up, the list is showing all the list items.
2.each list should have its own scrolling control. like if I scroll List "A" ,it should only be scrolled not the List"B" and vice versa. I have attached the code here . Thanks in advance.
mainscreen.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_LinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/request_Table"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#color/black"
android:orientation="vertical"
android:padding="20dp" >
<TableRow
android:id="#+id/request_Row1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/blue"
android:orientation="vertical" >
<TextView
android:id="#+id/request_TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="REQUESTS"
android:textColor="#color/white"
android:textStyle="bold" />
<ImageView
android:id="#+id/widget163"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</TableRow>
<TableRow
android:id="#+id/request_Row2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/gray"
android:orientation="horizontal" >
<ListView
android:id="#+id/request_ListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:scrollbarAlwaysDrawVerticalTrack="true" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/offered_Table"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#color/black"
android:orientation="vertical"
android:padding="20dp" >
<TableRow
android:id="#+id/offered_Row1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/blue"
android:orientation="horizontal" >
<TextView
android:id="#+id/offered_TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="OFFERED"
android:textColor="#color/white"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/offered_Row2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ListView
android:id="#+id/offered_ListView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/gray"
android:padding="5dp"
android:scrollbars="vertical"
android:smoothScrollbar="true" />
</TableRow>
</TableLayout>
</LinearLayout>
mainscreen.java
public class MainMenu extends Activity {
RequestAdapter mRequestAdapter;
ListView mListView_req ;
public class RequestData {
int icon;
String list_text;
public RequestData( String mText) {
super();
//this.icon=image;
this.list_text=mText;
}
}
private RequestData[] getListItems(){
Resources mRes = getResources();
String[] sListItems= mRes.getStringArray(R.array.request_list);
int listsize = sListItems.length;
RequestData[] obj = new RequestData[listsize];
int size = obj.length;
for(int i=0; i<size; i++){
obj[i] = new RequestData(sListItems[i]);
}
return obj;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu_screen);
mListView_req= (ListView)findViewById(R.id.request_ListView) ;
mRequestAdapter = new RequestAdapter(this,R.layout.listitem, getListItems());
mListView_req.setAdapter(mRequestAdapter);
}
class RequestAdapter extends ArrayAdapter{
private RequestData rList[]= null;
Context context;
public RequestAdapter(Context context, int textViewResourceId,
RequestData[] data) {
super(context, textViewResourceId, data);
// TODO Auto-generated constructor stub
this.rList= data;
}
#Override
public RequestData getItem(int position){
return rList[position];
}
#Override
public View getView(int pos, View convertView, ViewGroup parent) {
View row= convertView;
if (row== null){
row = getLayoyutinflater().inflate(R.layout.listitem,null);
}
else {
RequestData items = getItem(pos);
TextView text= (TextView) row.findViewById(R.id.List_Text);
if(text!= null){
text.setText(items.list_text);
}
}
return row;
}
listitem.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="150dp">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:id="#+id/text"/>
<ImageView>
android:layout_width="40dp"
android:layout_height="wrap_content"
android:src="#drawable/arrow"
android:layout_marginRight="10dp"/>
</LinearLayout>

Categories

Resources