2012-05-31 21 views
3

我有一個列表視圖的應用程序。列表視圖正常工作。當我想讓列表中的某些行標記時,問題就開始了。如果我按下它,我可以標記一行。但是,似乎沒有找到一種方法來標記任何初始化行。如何在ListView中標記視圖?

這是我的代碼:

listViewOfBluetooth = getListView(); 
setInitialEnabledDevices(); 
    listViewOfBluetooth.setOnItemClickListener(new OnItemClickListener() { 

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     String chosenBluetoothDevice = (String) ((TextView) view).getText(); 

     BluetoothEnableOrDisable(view, chosenBluetoothDevice); 
     Toast.makeText(getApplicationContext(), chosenBluetoothDevice, Toast.LENGTH_SHORT).show(); 
     editor.putString("bluetooth_name_from_list1", chosenBluetoothDevice); 
     editor.putBoolean("have_the_cars_bluetooth", true); 
     editor.commit(); 
     Intent intent = new Intent(List.this, ParkOGuardActivity.class); 
     startActivity(intent); 
     } 
    }); 
} 

public static void setInitialEnabledDevices(){ 
    int length = listViewOfBluetooth.getChildCount(); 
    View view = null; 
    String first = prefs.getString("bluetooth_name_from_list0", ""); 
    String second = prefs.getString("bluetooth_name_from_list1", ""); 
    String third = prefs.getString("bluetooth_name_from_list2", ""); 
    for(int i = 0; i < length; i++){ 
     view = listViewOfBluetooth.getChildAt(i); 
     if(view.equals(first) || view.equals(second) || view.equals(third)) { 
      view.setBackgroundColor(Color.GRAY); 
     } 
    }  
} 

我怎樣才能解決這個問題?

謝謝!

+0

你使用什麼適配器? – Shaiful

+0

我使用listadapter:setListAdapter(new ArrayAdapter (this,R.layout.list,arrayOfBluetoothDevicesNames)); – roiberg

+0

爲什麼不創建一個擴展ArrayAdapter的Adapter? 並重寫getView()方法。 – IvoryCirrus

回答

4

您可以通過使用自定義適配器來實現此目的。這是解決方法。

  • 初始化您的自定義適配器
  • 添加一些標誌顯着的設備名稱。
  • 覆蓋getView() &檢查標誌。並相應地設置列表項目的背景。

回覆,如果你沒有得到它或面臨任何複雜性。

更新:

這裏是一個樣本適配器。我沒有編譯代碼。所以可能會有一些錯誤。

import java.util.ArrayList; 

import android.content.Context; 
import android.graphics.Color; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

public class TestAdapter extends BaseAdapter 
{ 
    ArrayList<String> deviceNames; 
    ArrayList<Boolean> selected; 
    Context context; 

    public TestAdapter(Context context) 
    { 
     this.context = context; 
     deviceNames = new ArrayList<String>(); 
     selected = new ArrayList<Boolean>(); 
    } 

    public void addDeviceToList(String deviceName, boolean isSelected) 
    { 
     deviceNames.add(deviceName); 
     selected.add(isSelected); 
     notifyDataSetChanged(); 
    } 

    public int getCount() 
    { 
     return deviceNames.size(); 
    } 

    public Object getItem(int position) 
    { 
     return deviceNames.get(position); 
    } 

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

    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     TextView tv = new TextView(context); 
     tv.setText(deviceNames.get(position)); 
     if(selected.get(position) == true) 
     { 
      tv.setBackgroundColor(Color.parseColor("#ff0000")); 
     } 
     return tv; 
    } 
} 

現在創建適配器對象並將適配器設置爲listView。並通過調用addDeviceToList()方法添加單個項目。

+0

即時新在這裏...所以,它不是我清楚。 我希望你能給我看一些代碼,但會明白,如果它的要求太高... – roiberg

+0

我已經發布了我的代碼。檢查是否有幫助。 – Shaiful

0

這似乎討厭 但我認爲要在加載之前修改列表視圖內的意見就

的事情是,你的名單將不會有孩子,只要列表不顯示給用戶。所以你可以不修改視圖然後顯示給用戶

之前,但如果你真的需要在這樣低的水平的意見進行溝通,你可以嘗試滾動監聽器連接到您的列表:

@Override 
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
    for (int i = 0; i < visibleItemCount; i++) { 
     View child = getChildAt(i); 
     Now edit this view 

    } 
} 
相關問題