0

我已經使用數組適配器創建了包含圖像和文本的自定義lisview。我想突出顯示一行手動,但我似乎無法做到這一點。我曾嘗試使用setItemCheckedsetSelection(1);。我已經確認通過調用listView.setEnabled(false);來禁用觸摸事件,因爲我讀到如果啓用了觸摸事件,手動選擇可能不起作用。任何對此事的見解都將不勝感激。我在下面列出了我的源代碼。在使用arrayadapter的listview中突出顯示行

主要活動

public class MainActivity extends Activity 
{ 

    // GUI 
    int Update_Frequency = 1000; 
    double ack = 0; 


// Sensor Constants 
public static String temperature = "--"; 
public static String humidity = "--"; 
public static String lpg = "--"; 
public static String alcohol = "--"; 



// Layout 
ListView listView; 
ItemAdapter adapter; 


@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    //Initialize Interface 
    Model.LoadModel(); 
    listView = (ListView) findViewById(R.id.listView); 
    String[] ids = new String[Model.Items.size()]; 
    for (int i= 0; i < ids.length; i++) 
    {ids[i] = Integer.toString(i+1);} 
    this.adapter = new ItemAdapter(this,R.layout.row, ids); 
    listView.setAdapter(adapter); 
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
    listView.setEnabled(false); 
    listView.setItemChecked(1, true); 
    listView.setSelection(1); 



    Model.LoadModel(); 
    listView.setAdapter(adapter); 
    adapter.notifyDataSetChanged(); 
    GUI_Management(); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 


@Override 
public boolean onOptionsItemSelected(MenuItem item) 
{ 
    switch (item.getItemId()) 
    { 
     case R.id.action_connect: 
      break; 
     case R.id.action_disconnect: 
      break; 
     case R.id.action_settings: 
      break; 
     case R.id.action_about: 
      break; 
     case R.id.action_exit: 
      break; 
     default: 
      break; 
    } 

    return true; 
} 


private void GUI_Management() 
{ 
    new Thread() 
    { 
     public void run() 
     { 
      //Replace with a changeable variable 
      while (true) 
      { 
       try 
       { 
        runOnUiThread(new Runnable() 
        { 
         @Override 
         public void run() 
         { 
          Model.LoadModel(); 
          listView.setAdapter(adapter); 
          adapter.notifyDataSetChanged(); 
         } 
        }); 
        Thread.sleep(Update_Frequency); 
       } 
       catch (InterruptedException e) 
       { 
       } 
      } 
     } 
    }.start(); 
} 

} 

數組適配器

public class Model extends MainActivity 
{ 
    public static ArrayList<Item> Items; 

    public static void LoadModel() 
    { 
     Items = new ArrayList<Item>(); 
     Items.add(new Item(1, "temperature_icon.png", "Temperature/Humidity    " + temperature + "°F/" + humidity + "%")); 
     Items.add(new Item(2, "gas_icon.png", "LPG             " + lpg +" ppm")); 
     Items.add(new Item(3, "alcohol_icon.png", "Alcohol            " + alcohol + " ppm")); 
    } 

    public static Item GetbyId(int id) 
    { 
     for(Item item : Items) 
     { 
      if (item.Id == id) 
      { 
       return item; 
      } 
     } 
     return null; 
    } 
} 

class Item 
{ 
    public int Id; 
    public String IconFile; 
    public String Name; 

    public Item(int id, String iconFile, String name) 
    { 
     Id = id; 
     IconFile = iconFile; 
     Name = name; 
    } 
} 

class ItemAdapter extends ArrayAdapter<String> { 

    private final Context context; 
    private final String[] Ids; 
    private final int rowResourceId; 

    public ItemAdapter(Context context, int textViewResourceId, String[] objects) { 

     super(context, textViewResourceId, objects); 

     this.context = context; 
     this.Ids = objects; 
     this.rowResourceId = textViewResourceId; 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View rowView = inflater.inflate(rowResourceId, parent, false); 
     ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView); 
     TextView textView = (TextView) rowView.findViewById(R.id.textView); 

     int id = Integer.parseInt(Ids[position]); 
     String imageFile = Model.GetbyId(id).IconFile; 

     textView.setText(Model.GetbyId(id).Name); 
     // get input stream 
     InputStream ims = null; 
     try { 
      ims = context.getAssets().open(imageFile); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     // load image as Drawable 
     Drawable d = Drawable.createFromStream(ims, null); 
     // set image to ImageView 
     imageView.setImageDrawable(d); 
     return rowView; 

    } 


} 

回答

1

放setonclick聽者rootview對象,在getview方法,通過你膨脹,這將解決您的問題,並在click事件你可以改變rootview的背景以及更多你可以用它做的事情。

@Override 
     public View getView(int position, View convertView, ViewGroup parent) { 

      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      View rowView = inflater.inflate(rowResourceId, parent, false); 
      ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView); 
      TextView textView = (TextView) rowView.findViewById(R.id.textView); 

      int id = Integer.parseInt(Ids[position]); 
      String imageFile = Model.GetbyId(id).IconFile; 

      textView.setText(Model.GetbyId(id).Name); 

     if (position == selectedItem) 
     { 
      rootView /*or you can use any viewgroup*/ 
        .setBackgroundResource(R.drawable.background_dark_blue); 


     } 
     else 
     { 
      rootView 
        .setBackgroundResource(R.drawable.border02); 
     } 

       return rowView;  
     } 
    private int selectedItem; 
    public void setSelectedItem(int position) { 
     selectedItem = position; 
    } 

我知道這是不好的solutionbut可能這個幫助你出去,你可以使用setSelectedItem方法,突出行 並請使用靜態佔位符類的效率和使用靜態佔位符類位置字段獲得所選行的位置

+0

好的,但我該怎麼做手動。我不想在單擊時突出顯示一行,而是想自動突出顯示代碼中的一行。有任何想法嗎? – Willis

+0

哦,對不起,我的誤解,但你必須突出顯示在列表視圖中的所有行,那麼你必須查看此鏈接並將背景分配到您的根視圖。 http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape – mcd

+0

好的,謝謝。只是出於好奇,是否有一個原因我上面的方法不工作?因爲我已經看到了使用標準列表視圖實現的方法。謝謝 – Willis