问题
I have a DB in which id's are change after few rows, then same I'd in some rows then change I'd. What I want that, the same I'd values will be populated listview's in one line, then when the Ids are same, they are all in one row. And thus the next rows populate with same rows which have same ID's.
My DB columns and rows are as follows:
Verse_id -------words_id----------words_ar------ translate_bn--
1-----------------------1---------------Mamun-----------Assistant---
1-----------------------2--------------- Salam------------Operator------
1-----------------------3---------------John--------------Assistant------
2 ----------------------1--------------- Smith-------------Manager------
2-----------------------2---------------Roger--------------Director--------
3-----------------------1---------------Qoel---------------Helper----------
3-----------------------2---------------Saki---------------Mechanics-----
3-----------------------3----------------Ali-----------------Getman----------
I want this DB in three lines, as follows : (listview):
1. Mamun. Salam John
Assistant. Operator. Assistant
------------------------------------------------------------------
2. Smith. Roger.
Manager. Director
------------------------------------------------------------------
3. Qoel. Saki. Ali.
Helper. Mechanics. Getman
I have Tried in Two way : First :
private static final String PRIMARY_ID = "_id";
private static final String TABLE_NAME = "bywords";
private static final String FRIEND_ID = "verse_id";
private static final String WORDS_ID = "words_id";
private static final String WORDS_bN = "translate_bn";
private static final String WORDS_AR = "words_ar";
private SQLiteDatabase database;
private ArrayList<String> friends;
private ArrayList<String> trans1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
mContext = this;
ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
database = dbOpenHelper.openDataBase();
Cursor cursor = database.rawQuery("SELECT * FROM bywords", null);
ListView list4=(ListView)findViewById(R.id.list4) ;
String[] from = {WORDS_AR }; // _id mandatory
int[] to = new int[]{R.id.alquran_text};
CursorAdapter adapter = new SimpleCursorAdapter(
mContext,
R.layout.list_item,
cursor,
from,
to,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
list4.setAdapter(adapter);
And Then Second :
trans1 = new ArrayList<String>();
String[] allColumns2 = new String[] { FRIEND_ID,
BN_TRANS };
Cursor friendCursor2 = database.query(TABLE_NAME, allColumns2, null,
null, null, null, null);
if (friendCursor2 != null) {
friendCursor2.moveToFirst();
}
//return friendCursor;
if(!friendCursor2.isAfterLast()) {
do {
String name = friendCursor2.getString(1);
trans1.add(name);
} while (friendCursor2.moveToNext());
}
friendCursor2.close();
String [] bntrans1= new String[trans1.size()];
bntrans1 = trans1.toArray(bntrans1);
ListAdapter adapter = new ArrayAdapter<String>(
getApplicationContext(), R.layout.alert_row, seleted_route) {
ViewHolder holder;
Drawable icon;
class ViewHolder {
ImageView icon;
TextView title;
TextView title2;
}
public View getView(int position, View convertView,
ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater) getApplicationContext()
.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(
R.layout.alert_row, null);
holder = new ViewHolder();
//holder.icon = (ImageView) convertView
// .findViewById(R.id.icon4);
holder.title = (TextView) convertView
.findViewById(R.id.title4);
holder.title2 = (TextView) convertView
.findViewById(R.id.title2);
convertView.setTag(holder);
} else {
// view already defined, retrieve view holder
holder = (ViewHolder) convertView.getTag();
}
Drawable drawable = getResources().getDrawable(R.drawable.iqra1); //this is an image from the drawables folder
holder.title.setText(seleted_route[position]);
holder.title2.setText(seleted_route[position]);
//holder.icon.setImageDrawable(drawable);
return convertView;
}
};
By the second way I can populate two column in two text view in one rows. But what I want that the corresponding rows with same id of rows must populate in one line of listview with verse I'd. The id's whataver are same in rows may be 5, 10 or twenty rows same id. Whenever the same Id They are one line, When the Id change the newline begins.
来源:https://stackoverflow.com/questions/48360688/make-cursor-corresponding-array-with-multiple-rows-followed-by-same-row-ids