2016-10-28 7 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(リストビューアダプター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

[OK]を私はアダプターを使用してポストを実行する方法を取得する方法私はそれを使用することができますリストビューのコード私は私のコードウィットでエラーなしで追加してください –

関連する問題