-1

我想ListView在Android中构建一个。它应该有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);
4

2 回答 2

1

嗯,看起来您想创建一个显示:“1111”、“2111”等的 ListView。这是一个简单的方法来执行此操作根据其索引更改每行的颜色:

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;
        }
    }
}

为多个列更改此示例非常简单,您只需从具有多个成员数据的对象开始,选择适当的适配器并指定哪些列去哪里。例如:

  • SimpleCursorAdapter 绑定来自 SQLite 查询的数据。
  • SimpleAdapter 绑定来自通用 Map 对象的数据。
  • 您始终可以编写自己的适配器以尽量提高效率。

希望这有助于您入门!

于 2012-08-24T18:52:23.013 回答
0

您需要使用自定义列表适配器来满足您的要求。

步骤1

初始化您的 ListView

第2步

使用调用适配器

MultiColumnAdapter review = new MultiColumnAdapter(this,logDetails_cursor);
listview.setAdapter(review);

第 3 步

多列适配器

 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);

       }
     }

多列列表视图 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" /> 

希望能帮助到你

于 2012-08-24T13:07:30.033 回答