i'd like to start by saying that im quite new to android developing.
my question is how can i change my below examples to allow text on both the left and right side of each list item.
my aim is to have each list item have an item description on the left, and a numeric value for that description on the right.
all i have up to now is a list that shows the values which i want showing on the left of each item.
main java activity (NewtestActivity.java):
public class NewtestActivity extends Activity {
String[] exampleList = {
"Item 1",
"Item 2",
"Item 3",
"Test 4",
"Item 5",
"Test开发者_JAVA技巧 6",
"Item 7",
"Test 8"
//etc etc
};
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get an instance of your listview in code
ListView listview = (ListView) findViewById(R.id.list);
// Set the listview's adapter
listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, exampleList));
}
}
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:orientation="horizontal"
android:layout_width="280dp"
android:layout_height="fill_parent"
>
<ImageView
android:src="@drawable/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginLeft="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_marginLeft="5dp"
android:text="@string/app_name"
/>
</LinearLayout>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="200dp"
/>
</LinearLayout>
thanks for any help (:
With no direct link to your question, you can improve your xml code and your first LinearLayout
(header), by using the attribute android:drawableLeft
and android:drawablePadding
of your TextView
.
This accords you to place your icon on the left of your appName TextView.
- Extend ArrayAdapter
- Edit android.R.layout.simple_list_item_1 to contain the layout you want
Check this example
Define Custom Adapter like this and define your custum layout xml file as per your need.
import java.util.Vector;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class YourAdapter extends BaseAdapter{
private Context mContext;
private Vector mValuestobeShown;
private Vector mValuetext;
private Vector mValueNumber;
public YourAdapter (Context context, Vector text,Vector item, Vector number){
mContext = context;
mValuetext = text;
mValuestobeShown = item;
mValueNumber = number;
}
public int getCount() {
if(null != mValuestobeShown){
return mValuestobeShown.size();
}
return 0;
}
public Object getItem(int position) {
if(position < mValuestobeShown.size())
return mValuestobeShown.get(position);
else
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder ;
if(convertView == null){
LayoutInflater li =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.your_custom_layout, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.txt);
holder.item = (TextView) convertView.findViewById(R.id.item);
holder.num = (TextView) convertView.findViewById(R.id.num);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
try{
holder.item.setText(mValueType.get(position).toString());
//set other values.....
}catch(Exception e){ }
return convertView;
}
class ViewHolder {
TextView text;
TextView item;
TextView num;
}
}
精彩评论