2016-10-28 81 views
0

我想更改列表視圖行的背景,如果列值相同,則列表視圖背景會發生變化,這是我的代碼。如何更改列表視圖中行的背景顏色使用列表視圖列字段

public class FillList extends AsyncTask<String, String, String> { 
    String z = ""; 

    List<Map<String, String>> prolist = new ArrayList<Map<String, String>>(); 

    @Override 
    protected void onPreExecute() { 

    } 

    @Override 
    protected void onPostExecute(String r) { 

     Toast.makeText(getActivity(), r, Toast.LENGTH_SHORT).show(); 

     String[] from = {"C", "D", "E"}; 
     int[] views = {R.id.accname, R.id.openingbalance, R.id.closingblanace}; 
     final SimpleAdapter ADA = new SimpleAdapter(getActivity(), 
       prolist, R.layout.reportlayout, from, 
       views); 
     lstpro.setAdapter(ADA); 


    } 


    @Override 
    protected String doInBackground(String... params) { 

     try { 
      Connection con = connectionClass.CONN(); 
      if (con == null) { 
       z = "Error in connection with SQL server"; 
      } else { 
       final String stringlevel = getArguments().getString("level"); 
       String query = "select * from GLSCOADetail where AccLevelNo<='"+ stringlevel +"'"; 
       PreparedStatement ps = con.prepareStatement(query); 
       ResultSet rs = ps.executeQuery(); 


       ArrayList<String> data1 = new ArrayList<String>(); 
       while (rs.next()) { 
        Map<String, String> datanum = new HashMap<String, String>(); 
        datanum.put("C", rs.getString("AccName")); 
        datanum.put("D", rs.getString("OpeningBalance")); 
        datanum.put("E", rs.getString("ClosingBalance")); 

        prolist.add(datanum); 
       } 

       z = "Success"; 
      } 
     } catch (Exception ex) { 
      z = ex.getMessage(); 

     } 
     return z; 
    } 
} 

我想改變提交的,如果我的數據庫字段匹配列表視圖,然後排的顏色會改變,請幫助我的數據庫我的列表視圖的背景。

+0

您因而需要自定義列表Adapte: http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view – Sigiria

回答

0

主(活動)

public class Main extends Activity 
{ 
    private CustomAdapter adapter; 
    public ListView mListView = null; 
    public ArrayList<ListModel> CustomListViewValuesArr = new ArrayList<ListModel>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.listview_layout); 
     mListView = (ListView) findViewById(R.id.list); 
     setListData(); 
     adapter = new CustomAdapter(this, CustomListViewValuesArr); 
     mListView.setAdapter(adapter); 
     adapter.notifyDataSetChanged(); 

    } 

    /****** Function to set data in ArrayList *************/ 
    public void setListData() 
    { 
     CustomListViewValuesArr.add(new ListModel("No1")); 
     CustomListViewValuesArr.add(new ListModel("No2"));//same 
     CustomListViewValuesArr.add(new ListModel("No3")); 
     CustomListViewValuesArr.add(new ListModel("No4")); 
     CustomListViewValuesArr.add(new ListModel("No2"));//same 

    } 

} 

listview_layout.xml(活動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" > 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent"/> 

</RelativeLayout> 

CustomAdapter(適配器)

public class CustomAdapter extends BaseAdapter 
{ 

    /*********** Declare Used Variables *********/ 
    private Activity mActivity; 
    private ArrayList<ListModel> mList; 
    private static LayoutInflater mInflater = null; 
    private ListModel tempValues = null; 
    private int i=0; 

    /************* CustomAdapter Constructor *****************/ 
    public CustomAdapter(Activity a, ArrayList d) { 

     /********** Take passed values **********/ 
     mActivity = a; 
     mList = d; 
     /*********** Layout inflator to call external xml layout() ***********/ 
     mInflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    /******** What is the size of Passed Arraylist Size ************/ 
    public int getCount() { 

     if(mList.size()<=0) 
      return 1; 
     return mList.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

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

    /********* Create a holder Class to contain inflated xml file elements *********/ 
    public static class ViewHolder 
    { 
     public TextView text; 

    } 

    /****** Depends upon mList size called for each row , Create each ListView row *****/ 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     View vi = convertView; 
     ViewHolder holder; 
     if(convertView==null){ 

      /****** Inflate tabitem.xml file for each row (Defined below) *******/ 
      vi = mInflater.inflate(R.layout.tabitem, null); 

      /****** View Holder Object to contain tabitem.xml file elements ******/ 

      holder = new ViewHolder(); 
      holder.text = (TextView) vi.findViewById(R.id.textView); 

      /************ Set holder with LayoutInflater ************/ 
      vi.setTag(holder); 
     } 
     else 
      holder=(ViewHolder)vi.getTag(); 

     if(mList.size() <= 0) 
     { 
      holder.text.setText("No Data"); 
     } 
     else 
     { 
      tempValues = null; 
      tempValues = (ListModel) mList.get(position); 
      holder.text.setText(tempValues.getName()); 
     } 

     if(isDuplicate(tempValues.getName()))// this Method to check duplicate. 
     { 
      // Set a background color for ListView regular row/item 
      vi.setBackgroundColor(Color.parseColor("#FFB6B546")); 
     } 
     else 
     { 
      // Set the background color for alternate row/item 
      vi.setBackgroundColor(Color.parseColor("#FFCCCB4C")); 
     } 
     return vi; 
    } 

    private boolean isDuplicate(String item) 
    { 
     int count = 0; 
     for (int i = 0; i < mList.size(); i++) 
     { 
      ListModel listModel = mList.get(i); 
      if(item.equals(listModel.getName())) 
       count++; 
     } 
     if(count > 1) 
      return true; 
     else 
      return false; 
    } 
} 

的ListModel(模型類)

public class ListModel { 
    private String name =""; 

    /*********** Set Methods ******************/ 

    public ListModel(String name) 
    { 
     this.name = name; 
    } 

    public void setName(String CompanyName) 
    { 
     this.name = CompanyName; 
    } 

    /*********** Get Methods ****************/ 

    public String getName() 
    { 
     return this.name; 
    } 

} 

的TabItem(ListView的適配器XML)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:text="TextView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:id="@+id/textView" /> 
</LinearLayout> 
+0

notifydatasetchanged(),其中我打電話..? –

+0

將列表添加到適配器後調用notifydatasetchanged()。 –

+0

好的,但我在後使用適配器執行get view方法我該如何使用它請發送給我的列表視圖的代碼,我在我的代碼中添加機智沒有錯誤 –

相關問題