複数列のリストビューがあります...選択またはクリックされた行アイテムの背景色を変更したい。stackoverflowに関するさまざまな記事を参照しましたが、役に立たないようです...OnItemClickで行ったことは.setBackgroundColorです。色を変更した行の意味で色の変更を記憶したいので、セレクターを使用できません。アプリケーションを再起動すると、変更を記憶する必要があります...フラグ値をデータベースに入れて、getViewで色を設定できますが初めてそれを行う方法は?これがコードです.....
public class AdminMultiColumn extends Activity
{
private ArrayList<HashMap<String,String>> list;
ArrayList<String> al;
AlertDialog.Builder alertDialogBuilder;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.admin_checkoutlist);
lview = (ListView)findViewById(R.id.admin_list);
populateList();
Admin_checkoutlist adapter = new Admin_checkoutlist(this,list);
lview.setAdapter(adapter);
}
private void populateList() {
list = new ArrayList<HashMap<String,String>>();
al = new ArrayList<String>();
testday = c.get(Calendar.DAY_OF_MONTH);
testmonth = c.get(Calendar.MONTH)+1;
testyear = c.get(Calendar.YEAR);
dateget = testday+"-"+testmonth+"-"+testyear;
mdb.open();
va.open();
Cursor c1 = va.getNameTimeinTimeout(dateget);
DatabaseUtils.dumpCursor(c1);
if(c1.moveToFirst())
{
do{
idfromdb = c1.getString(0);
al.add(idfromdb);
namefromdb = c1.getString(1);
al.add(namefromdb);
timefromdb = c1.getString(2);
al.add(timefromdb);
timeoutfromdb = c1.getString(3);
al.add(timeoutfromdb);
}while(c1.moveToNext());
}
c1.close();
va.close();
mdb.close();
Log.d("pavan","data retrived "+namefromdb+" "+timeoutfromdb+" "+timefromdb+" "+idfromdb);
for(int i = 0;i<al.size();i=i+4)
{
temp = new HashMap<String,String>();
temp.put(FIRST_COLUMN, al.get(i));
temp.put(SECOND_COLUMN, al.get(i+1));
temp.put(THIRD_COLUMN, al.get(i+2));
temp.put(FOURTH_COLUMN, al.get(i+3));
Log.d("pavan","test here for admin");
list.add(temp);
}
}
//adapter class goes here////////////
public class Admin_checkoutlist extends BaseAdapter implements OnItemClickListener
{
public ArrayList<HashMap<String,String>> list;
Activity activity;
public Admin_checkoutlist(Activity activity, ArrayList<HashMap<String,String>> list)
{
super();
this.activity = activity;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int pos) {
// TODO Auto-generated method stub
return list.get(pos);
}
@Override
public long getItemId(int pos) {
// TODO Auto-generated method stub
return 0;
}
public class ViewHolder
{
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
TextView txtFOURTH;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.temp, null);
holder = new ViewHolder();
holder.txtFirst = (TextView)convertView.findViewById(R.id.uid);
holder.txtSecond = (TextView)convertView.findViewById(R.id.nametext);
holder.txtThird = (TextView)convertView.findViewById(R.id.checkintext);
holder.txtFOURTH = (TextView)convertView.findViewById(R.id.checkouttext);
convertView.setTag(holder);
lview.setOnItemClickListener(this);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> map = list.get(position);
holder.txtFirst.setText(map.get(FIRST_COLUMN));
holder.txtSecond.setText(map.get(SECOND_COLUMN));
holder.txtThird.setText(map.get(THIRD_COLUMN));
holder.txtFOURTH.setText(map.get(FOURTH_COLUMN));
return convertView;
}//getview ends here.
@Override
public void onItemClick(final AdapterView<?> parent, final View view, int pos,
long id) {
// TODO Auto-generated method stub
Object listItem = parent.getFirstVisiblePosition()+pos+1;
//lview.getChildAt(pos).setBackgroundColor(R.color.blue);
view.setBackgroundColor(pos);
idtosend = listItem.toString();
Log.d("pavan","id is"+idtosend);
/////alert displaing block starts here////////////////////////////////
alertDialogBuilder = new AlertDialog.Builder(AdminMultiColumn.this);
alertDialogBuilder.setTitle("Alert");
alertDialogBuilder
.setMessage("Check out now")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
Toast.makeText(getApplicationContext(), "Checkout successfull", Toast.LENGTH_LONG).show();
testhour = c.get(Calendar.HOUR_OF_DAY);
testmin = c.get(Calendar.MINUTE);
testsec = c.get(Calendar.SECOND);
timings = testhour+":"+testmin+":"+testsec;
mdb.open();
va.open();
va.upDate(idtosend,timings);
Toast.makeText(getApplicationContext(), "update the checkout options "+timings, Toast.LENGTH_SHORT).show();
va.close();
mdb.close();
/*Intent go = new Intent(AdminMultiColumn.this,MultiColumn.class);
startActivity(go);*/
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
///////alert displaing block ends here//////////////////////////////////
}
}
}
私が今行ったことは、getViewメソッド内で、convertViewを取得し、背景リソースを色に設定していますが、これも機能していません...以下はコードです...
if(holder.txtFOURTH!=null)
{
convertView.setBackgroundColor(R.drawable.image_border);
}
else
{
Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
}