I've been busy and couldn't update much, so I updated it after a long time. This time, I wanted to change the line color of ListView in the app I'm currently working on, so I'll leave a note about it.
I have omitted various contents, but the purpose is It feels like "when getting data from the DB and preparing the List display, I will color the row of the currently selected data so that it is easy to understand."
sample.java
list = findViewById(R.id.splist_array);
SampleList = new ArrayList<HashMap<String,String>>();
int i = 0;
flg_pos = -1;
//list
String NAME,ID,KGN,FLG;
//DB
helper = new DBHelper(getApplicationContext());
HashMap<String,String> bList = new HashMap<String,String>();
try{
db = helper.getWritableDatabase();
//Read the number
RecordCount = DatabaseUtils.queryNumEntries(db,TABLE);
if(RecordCount == 0){
flg = false;
}else{
//Read
c = db.rawQuery(SAMPLE_SQL,null);
while(c.moveToNext()){
NAME = c.getString(c.getColumnIndex(COL_NAME));
ID = String.valueOf(c.getInt(c.getColumnIndex(COL_ID)));
KGN = c.getString(c.getColumnIndex(COL_KGN));
FLG = c.getString(c.getColumnIndex(COL_FLG));
//Judge whether it is selected here and give it a Position.
if(FLG.equals("t")){
flg_pos = i;
}
bList = new HashMap<String,String>();
bList.put(COL_ID,ID);
bList.put(COL_NAME,NAME);
bList.put(COL_KGN,KGN);
SampleList.add(bList);
i++;
}
}
}catch(SQLiteException e){
e.printStackTrace();
}
After the above acquisition is completed, it is finally set in Adapter.
sample.java
if(SampleList != null){
//GetView when setting the Adapter()Override so that can be called.
//getView(--)Is called repeatedly for the set line, so use that. (Position=From 0)
adapter = new SimpleAdapter(MainActivity.this,SampleList,
R.layout.list_row,FROM,TO){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position,convertView, parent);
//Determine the line you want to color here
//flg_pos is the line you want to color
if(flg_pos == position){
//Color the view parts.
view.setBackgroundColor(Color.parseColor("#4169e1"));
listText1 = view.findViewById(R.id.btext1); //id
listText2 = view.findViewById(R.id.btext2); //name
listText3 = view.findViewById(R.id.btext3); //kgn
listText1.setTextColor(Color.WHITE);
listText2.setTextColor(Color.WHITE);
listText3.setTextColor(Color.WHITE);
}
return view;
}
};
list.setAdapter(adapter);
}else if(flg == false){
//If there is nothing in the List, I want to display something.
}else{
Log.e("ListError","Missing List...");
Toast.makeText(this,"ListReadingError...",Toast.LENGTH_SHORT);
}
It was easy. Rather.
I tried various googles, but there were many articles such as creating methods, and this was the easiest way to implement it smoothly. Well, if you want to customize something else, I feel like I can do it a little more. I'm sorry that there is no completed drawing or contents such as List_row. I don't have much time, so I'll come to update it here and there.
This may be easier to understand. Octagonal Research Institute. Thank you very much. It was helpful. (http://www.hakkaku.net/hakkaker_blog/20090831-590)
Recommended Posts