我想在Android中构建ListView
。它应该multiple coloums
,并且可能change color of only some rows
。我试过这个,但我认为它调用了LinearLayout的 toString()。我没有得到text inside the TextView
。我好几天都试过了。请帮忙。
下面的代码只是实际项目的实验。
public class EventActivity extends ListActivity {
List<Event> events2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.event_layout);
ListView listView = (ListView) findViewById(R.id.listEvents);
listView = getListView();
TextView t1 = new TextView(getBaseContext());
TextView t2 = new TextView(getBaseContext());
TextView t3 = new TextView(getBaseContext());
TextView t4 = new TextView(getBaseContext());
TextView t5 = new TextView(getBaseContext());
TextView t6 = new TextView(getBaseContext());
TextView t7 = new TextView(getBaseContext());
t1.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
t2.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
t3.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
t4.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
t5.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
t6.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
t1.setText("1111");
t2.setText("2111");
t3.setText("3111");
t4.setText("4111");
t5.setText("5111");
t6.setText("6111");
t7.setText("7111");
t1.setBackgroundColor(Color.GREEN);
t4.setBackgroundColor(Color.GREEN);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(t1);
ll.addView(t2);
ll.addView(t3);
ll.addView(t4);
ll.addView(t5);
ll.addView(t6);
ll.addView(t7);
ArrayList<LinearLayout> lllist = new ArrayList<LinearLayout>();
lllist.add(ll);
ArrayAdapter<LinearLayout> eventAdapter = new ArrayAdapter<LinearLayout>(EventActivity.this,
android.R.layout.simple_list_item_1,lllist );
listView.setAdapter(eventAdapter);
答案 0 :(得分:1)
嗯,看起来你想制作一个显示的ListView:“1111”,“2111”等。这是一个简单的方法和根据它改变每一行的颜色指数:
public class Example extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] array = new String[] {"1111", "2111", "3111", "4111", "5111", "6111"};
ColorfulArrayAdapter<String> adapter = new ColorfulArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
}
public class ColorfulArrayAdapter<T> extends ArrayAdapter<T> {
public ColorfulArrayAdapter(Context context, int textViewResourceId, T[] objects) {
// There are many more constructors available from ArrayAdapter, consult the documentation for specifics!
super(context, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
// Change the background from its position, I added three choices
switch(position % 3) {
case 0: // Red
view.setBackgroundColor(0xffff0000);
break;
case 1: // Green
view.setBackgroundColor(0xff00ff00);
break;
case 2: // Blue
view.setBackgroundColor(0xff0000ff);
}
return view;
}
}
}
为多列更改此示例很简单,您只需要从具有多个成员数据的对象开始,选择适当的适配器并指定哪些列到哪里。例如:
希望有助于您入门!
答案 1 :(得分:0)
您需要根据您的要求使用自定义列表适配器。
<强>步骤1 强>
初始化您的ListView
第2步
使用
调用适配器MultiColumnAdapter review = new MultiColumnAdapter(this,logDetails_cursor);
listview.setAdapter(review);
第3步
MultiColumnAdapter
class MultiColumnAdapter extends CursorAdapter {
LayoutInflater mInflater;
SimpleDateFormat sdf;
RequestLogAdapter(Context context, Cursor cursor) {
super(context, cursor);
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Get your details here
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
//Create xml for multicolumnlistview
return mInflater.inflate(R.layout.mulitcolumnlistview, parent, false);
}
@Override
public void bindView(View row, Context context, Cursor cursor) {
//Rows of multicolumn listview
TextView reqdate = (TextView) row
.findViewById(R.id.firstcolumn);
TextView reqtime = (TextView) row
.findViewById(R.id.secondcolumn);
TextView reqfromlocation = (TextView) row
.findViewById(R.id.thirdcolumn);
String text1 = cursor.getString(i_requestdate);
reqdate.setText(yourtext);
String text2 = cursor.getString(i_requesttime);
reqtime.setText(timeRequested);
String text3 = cursor.getString(i_requestfromlocation);
reqfromlocation.setText(yourtext);
}
}
mulitcolumnlistview ListView Xml
您可以在此设置背景颜色。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">
<TextView android:id="@+id/column1"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:textColor="@color/White"
android:layout_marginLeft="5dip"
android:layout_weight="1"
android:textSize="16sp" />
<TextView android:id="@+id/column2"
android:layout_width="60dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp"
android:textColor="@color/White"
android:maxLines="1" />
<TextView android:id="@+id/column3"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:textSize="17sp"
android:textColor="@color/White"
android:layout_weight="2" />
希望有所帮助