我有两个自定义适配器,我想将它合并到一个适配器中 适配器携带名称和mac信息 我见过很多问题,但没有明白要做什么 谢谢
MyAdapter.java
public class MyAdapter extends ArrayAdapter<DataPro> {
public MyAdapter(Context context, int resource, List<DataPro> collect) {
super(context, resource, collect);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(getContext());
DataPro item = getItem(position);
textView.setTextColor(Color.BLACK);
textView.setText(item.Name+" : "+item.Mac);
return textView;
}
}
MyAdapter2.java
public class MyAdapter2 extends ArrayAdapter<Datapro2> {
public MyAdapter2(Context context, int resource, List<Datapro2> collect1) {
super(context, resource, collect1);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(getContext());
Datapro2 item = getItem(position);
textView.setTextColor(Color.BLACK);
textView.setText(item.Name);
return textView;
}
}
在这个下面的java类中,我在两个不同的列表视图中显示数据,我想在一个
中显示它public class Main2Activity extends AppCompatActivity {
ListView listOfDatabaseObject;
ListView listView;
SQLiteDatabase sqLiteDatabase;
UserDbHelper userDbHelper;
List<DataPro> result;
List<Datapro2> results;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
listOfDatabaseObject = (ListView)findViewById(R.id.listView);
listView = (ListView)findViewById(R.id.listView2);
userDbHelper = new UserDbHelper(getApplicationContext());
sqLiteDatabase =userDbHelper.getReadableDatabase();
result = userDbHelper.getItemFromDatabase(sqLiteDatabase);
MyAdapter adapter = new MyAdapter(getApplicationContext(), 0, result);
listOfDatabaseObject.setAdapter(adapter);
adapter.notifyDataSetChanged();
results = userDbHelper.getItemFromDatabase2(sqLiteDatabase);
MyAdapter2 adapter2 = new MyAdapter2(getApplicationContext(), 0, results);
listView.setAdapter(adapter2);
adapter.notifyDataSetChanged();
}
}
DataPro.java
public class DataPro {
String Name;
String Mac;
public DataPro(String Name, String Mac)
{
this.Name=Name;
this.Mac=Mac;
}
}
Datapro2.java
public class Datapro2 {
String Name;
public Datapro2(String Name)
{
this.Name=Name;
}
}
DataBaseclass.java
public class UserDbHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "STUDENTINFO.DB";
public static final int DB_VERSION = 1;
String query = "SELECT * FROM " +MainActivity.collect+ " where " +Coloumn.NewInfo.MAC+ " in(select "+Coloumn.NewInfo.MAC+ " FROM " +MainActivity.collect2+");";
String query2 = "SELECT " + Coloumn.NewInfo.NAME + " FROM " +MainActivity.collect+ " where " +Coloumn.NewInfo.MAC+ " not in(select "+Coloumn.NewInfo.MAC+ " FROM " +MainActivity.collect2+");";
public UserDbHelper(Context context)
{
super(context,DB_NAME,null,DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}
public void AddInformation(String name,String mac,SQLiteDatabase sqLiteDatabase)
{
ContentValues contentValues = new ContentValues();
contentValues.put(Coloumn.NewInfo.NAME,name);
contentValues.put(Coloumn.NewInfo.MAC,mac);
sqLiteDatabase.insert(MainActivity.collect,null,contentValues);
}
public void AddInformation2(String name,String mac,SQLiteDatabase sqLiteDatabase)
{
ContentValues contentValues = new ContentValues();
contentValues.put(Coloumn.NewInfo.NAME,name);
contentValues.put(Coloumn.NewInfo.MAC,mac);
sqLiteDatabase.insert(MainActivity.collect2,null,contentValues);
}
public List<DataPro> getItemFromDatabase(SQLiteDatabase sqLiteDatabase) {
List<DataPro> result = new ArrayList<>();
Cursor c = sqLiteDatabase.rawQuery(query,null);
while (c.moveToNext()) {
result.add(
new DataPro(
c.getString(c.getColumnIndex("name")),
c.getString(c.getColumnIndex("mac"))
)
);
}
c.close();
return result;
}
public List<Datapro2> getItemFromDatabase2(SQLiteDatabase sqLiteDatabase) {
List<Datapro2> result = new ArrayList<>();
Cursor c = sqLiteDatabase.rawQuery(query2,null);
while (c.moveToNext()) {
result.add(
new Datapro2(
c.getString(c.getColumnIndex("name"))
)
);
}
c.close();
return result;
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
答案 0 :(得分:1)
您只能使用1个适配器,并且可以使用任何标记(整数)以不同方式管理它们。
对于Ex。 :
在DataProWrapper类中获取额外的int。
public class{
public DataPro dataProObject;
public int type;
}
实施例。 public int type = 0;
(第一个适配器设置为0,第二个适配器设置为1)
在对象(DataProWrapper列表)中设置,您在Adapter的构造函数中发送。
public class MyAdapter extends ArrayAdapter<DataPro> {
public MyAdapter(Context context, int resource, List<DataProWrapper> collect) {
super(context, resource, collect);
// save to use in getView() method
this.collect = collect;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// get type value
DataPro object = collect.get(position).dataProObject;
if(object.type == 0){
// first adapter
TextView textView = new TextView(getContext());
DataPro item = getItem(position);
textView.setTextColor(Color.BLACK);
textView.setText(item.Name+" : "+item.Mac);
return textView;
}else{
//Second adapter
TextView textView = new TextView(getContext());
Datapro2 item = getItem(position);
textView.setTextColor(Color.BLACK);
textView.setText(item.Name);
return textView;
}
}
}