gpt4 book ai didi

android - 如何将两个 ArrayAdapter 合并为一个并在一个 ListView 中显示

转载 作者:行者123 更新时间:2023-11-30 00:47:39 27 4
gpt4 key购买 nike

我有两个自定义适配器,我想将它们合并到一个适配器中Adapter携带名称和mac信息我看到了很多问题,但不清楚该怎么做在此之前致谢

我的适配器.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 类中,我在两个不同的 ListView 中显示数据,我想在一个 ListView 中显示它

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;
}
}

数据库类.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) {

}
}

最佳答案

您只能使用 1 个适配器,并且可以使用任何标签(整数)以不同方式管理它们。

例如。 :

在 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;
}
}
}

关于android - 如何将两个 ArrayAdapter 合并为一个并在一个 ListView 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41532613/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com