2

我有一个从数据库生成的 listView,有没有办法为列表中的每个“部分”制作背景颜色或图片:如果“提示”中的第一个符号是“W”,则背景应该是绿色,如果它的“L”应该是红色的

我在想这样的事情,但我不知道把它放在哪里,因为它必须在每个部分中完成:

tipvalue = BetsDbAdapter.KEY_TIP;
    //Here to split the value to gain only "W" or "L"
String arrtip[] = tipvalue.split(" ", 2);
temptip = arrtip[0];
    //then set background
if (temptip.equals("W")) {
    getWindow().getDecorView().setBackgroundColor(Color.GREEN);

左边是我的 listView,右边是 xml 文件,用于在 listview 中创建每个“部分”

列表显示 存储的赌注.xml

这是我的数据库: 在此处输入图像描述

这是生成 listView 的代码

public class StoredBets extends Activity {

    private BetsDbAdapter dbHelper;
    private SimpleCursorAdapter dataAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.results);
        dbHelper = new BetsDbAdapter(this);
        dbHelper.open();    
    }

    @Override
    public void onStart(){
        super.onStart();
        displayListView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.results, menu);
        return true;
    }

    public void testknap1(View v) {
         Intent myIntent = new Intent(StoredBets.this, Overview.class);        
           startActivity(myIntent);     
    }

    private void displayListView() {
    Cursor cursor = dbHelper.fetchAllStats();

    String[] columns = new String[] {
            BetsDbAdapter.KEY_SMATCH,
            BetsDbAdapter.KEY_TIP,
            BetsDbAdapter.KEY_BETAMOUNT,
            BetsDbAdapter.KEY_BODDS
          };


      // the XML defined views which the data will be bound to
      int[] to = new int[] { 
        R.id.smatch,
        R.id.tip,
        R.id.bodds,
        R.id.betamount,
      };

      // create the adapter using the cursor pointing to the desired data 
      //as well as the layout information
      dataAdapter = new SimpleCursorAdapter(
        this, R.layout.storedbets, 
        cursor, 
        columns, 
        to,
        0);

      ListView listView = (ListView) findViewById(R.id.listView2);
      // Assign adapter to ListView
      listView.setAdapter(dataAdapter);

      listView.setOnItemClickListener(new OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> listView, View view, 
             int position, long id) {
           // Get the cursor, positioned to the corresponding row in the result set
           Cursor cursor = (Cursor) listView.getItemAtPosition(position);

           // Get the state's capital from this row in the database.
           String betMatch = 
            cursor.getString(cursor.getColumnIndexOrThrow("smatch"));
           Toast.makeText(getApplicationContext(),
             betMatch, Toast.LENGTH_SHORT).show();

           Intent myIntent = new Intent(StoredBets.this, SetWinVoidLoss.class);
           myIntent.putExtra("id", id);
           startActivity(myIntent);      
           }
          });
    }
}

编辑2:

@amal

这是带有列表视图的布局(results.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".StoredBets" >

    <ListView
        android:id="@+id/listView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignTop="@+id/button1" >

    </ListView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"

        android:layout_marginLeft="180dp"

        android:onClick="testknap1"
        android:text="Button" />

</RelativeLayout>
4

2 回答 2

0

你可以从我的代码中得到一个公平的想法,
用于显示“curSelected”项目具有不同背景的 ListViews 的代码片段:

final ListView lv = (ListView)findViewById(R.id.lv);
lv.setAdapter(new BaseAdapter()
{
public View getView(int position, View convertView, ViewGroup parent)
{
    if (convertView == null)
    {
        convertView = new TextView(ListHighlightTestActivity.this);
        convertView.setPadding(10, 10, 10, 10);
        ((TextView)convertView).setTextColor(Color.WHITE);
    }

    convertView.setBackgroundColor((position == curSelected) ? 
        Color.argb(0x80, 0x20, 0xa0, 0x40) : Color.argb(0, 0, 0, 0));
    ((TextView)convertView).setText((String)getItem(position));

    return convertView;
}

public long getItemId(int position)
{
    return position;
}

public Object getItem(int position)
{
    return "item " + position;
}

public int getCount()
{
    return 20;
}
});
于 2013-06-05T07:48:09.457 回答
0

是一个关于如何为列表视图编写适配器的教程。amal 是对的,您可以在方法中更改列表视图中的单独项目getView()

于 2013-06-05T07:51:47.807 回答