gpt4 book ai didi

android - 在 Fragment 中使用 ListActivity

转载 作者:行者123 更新时间:2023-11-29 21:11:40 25 4
gpt4 key购买 nike

下面的代码正在运行在单独项目中实现的自定义 ListView 。

public class MainActivity extends ListActivity implements OnTouchListener{

private MyCustomAdapter mAdapter;
Activity temp = this;
String []s = new String[500];
ArrayList<GS> q = new ArrayList<GS>();
ListView lv;
int count=0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

DBAdapter db = DBAdapter.getDBAdapter(getApplicationContext());
if (!db.checkDatabase())
{
db.createDatabase(getApplicationContext());
}
db.openDatabase();

q = db.getData();

mAdapter = new MyCustomAdapter();


mAdapter.addSeparatorItem(new ContentWrapper(q.get(0).getA_name(),null));
mAdapter.addItem(new ContentWrapper(q.get(0).getAS_name(), q.get(0).getDesc_art()));


for (int i = 1; i < 460; i++) {

if (!(q.get(i).getA_name().trim().equals(q.get(i-1).getA_name().trim()))) {

mAdapter.addSeparatorItem(new ContentWrapper(q.get(i).getA_name(), null));

}
mAdapter.addItem(new ContentWrapper(q.get(i).getAS_name(), q.get(i).getDesc_art()));

}

setListAdapter(mAdapter);

}

//Adapter Class
private class MyCustomAdapter extends BaseAdapter {

private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

private ArrayList<ContentWrapper> mData = new ArrayList<ContentWrapper>();
private LayoutInflater mInflater;

private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public void addItem(ContentWrapper value) {
mData.add(value);
notifyDataSetChanged();
}

public void addSeparatorItem(ContentWrapper value) {
mData.add(value);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}

public ContentWrapper getItem(int position) {
return mData.get(position);
}
@Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}

@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}

public int getCount() {
return mData.size();
}

public long getItemId(int position) {
Log.v("getItemId Position", ""+position);
return position;

}

public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.activity_main1, null);
holder.textView = (TextView)convertView.findViewById(R.id.text);

break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.activity_main2, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
count++;
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
} holder.textView.setText(mData.get(position).getItem());

if (type == TYPE_ITEM) {
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder x = new AlertDialog.Builder(temp);
Log.v("position",""+position);
x.setIcon(R.drawable.ic_launcher)

// .setTitle(q.get(position-count).getAS_name())
.setTitle(mData.get(position).getItem())

// .setMessage(q.get(position-count).getDesc_art())
.setMessage(mData.get(position).getItemDescription())

.setCancelable(true)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg,
int arg1) {
}
});
AlertDialog a = x.create();
a.show();
}
});
} else {
holder.textView.setOnClickListener(null);
}

return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}

public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}

}

现在我希望它在我的应用程序中以同样的方式运行 with Fragment。我只知道使用 fragment = new ContentsFragment(); 在 fragment 中启动它。它是否应该使用默认构造函数扩展 ListFragment & 我已经膨胀了另一个 Activity 的 onCreateView(...)

我是 fragment 的新手,我不知道代码中应该更改哪些内容。

请帮忙!

编辑:

我正在展示我尝试过的实现代码,我收到了代码中指定的 4 个错误:

public class ContentsFragment extends Fragment  implements OnTouchListener{

private MyCustomAdapter mAdapter;
Activity temp = this;// error: Type mismatch: cannot convert from ContentsFragment to Activity

String []s = new String[500];
ArrayList<GS> q = new ArrayList<GS>();
ListView lv;
int count=0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

DBAdapter db = DBAdapter.getDBAdapter(getApplicationContext());//error :The method getApplicationContext() is undefined for the type ContentsFragment
if (!db.checkDatabase())
{
db.createDatabase(getApplicationContext());//error : The method getApplicationContext() is undefined for the type ContentsFragment
}
db.openDatabase();

q = db.getData();

mAdapter = new MyCustomAdapter();


mAdapter.addSeparatorItem(new ContentWrapper(q.get(0).getA_name(),null));
mAdapter.addItem(new ContentWrapper(q.get(0).getAS_name(), q.get(0).getDesc_art()));


for (int i = 1; i < 460; i++) {

if (!(q.get(i).getA_name().trim().equals(q.get(i-1).getA_name().trim()))) {

mAdapter.addSeparatorItem(new ContentWrapper(q.get(i).getA_name(), null));

}
mAdapter.addItem(new ContentWrapper(q.get(i).getAS_name(), q.get(i).getDesc_art()));

}

setListAdapter(mAdapter); //error : The method getApplicationContext() is undefined for the type ContentsFragment

}

//Adapter Class
private class MyCustomAdapter extends BaseAdapter {

private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

private ArrayList<ContentWrapper> mData = new ArrayList<ContentWrapper>();
private LayoutInflater mInflater;

private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public void addItem(ContentWrapper value) {
mData.add(value);
notifyDataSetChanged();
}

public void addSeparatorItem(ContentWrapper value) {
mData.add(value);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}

public ContentWrapper getItem(int position) {
return mData.get(position);
}
@Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}

@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}

public int getCount() {
return mData.size();
}

public long getItemId(int position) {
Log.v("getItemId Position", ""+position);
return position;

}

public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.activity_main1, null);
holder.textView = (TextView)convertView.findViewById(R.id.text);

break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.activity_main2, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
count++;
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
} holder.textView.setText(mData.get(position).getItem());

if (type == TYPE_ITEM) {
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder x = new AlertDialog.Builder(temp);
Log.v("position",""+position);
x.setIcon(R.drawable.ic_launcher)

// .setTitle(q.get(position-count).getAS_name())
.setTitle(mData.get(position).getItem())

// .setMessage(q.get(position-count).getDesc_art())
.setMessage(mData.get(position).getItemDescription())

.setCancelable(true)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg,
int arg1) {
}
});
AlertDialog a = x.create();
a.show();
}
});
} else {
holder.textView.setOnClickListener(null);
}

return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}

public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}

最佳答案

setListAdapterListFragment 的一个方法。所以你需要扩展 ListFragment 来使用它。

改变这个

mAdapter = new MyCustomAdapter();

mAdapter = new MyCustomAdapter(getActivity());

然后

public MyCustomAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}

关于android - 在 Fragment 中使用 ListActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22593203/

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