我已经阅读了几篇有关片段的教程,但我无法将查询显示在List Fragment中。
以下是我需要转换以在片段活动中使用的原始类。我的QueryDisplay
课程中有很多内容,我不知道要从中删除什么以及将它放在哪里以使其正常工作。主要是,我不知道如何在下面的片段活动中调用我的ListView类。
谢谢 - 提前为您提供帮助。代码也非常有用!!
这是查询SQLite数据库的ListActivity,下面是id片段类:
public class QueryDisplay extends ListActivity {
protected TextView activityTitle;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File dbfile = new File(extStorageDirectory
+ "/myco/myapp/dB/myapp.db");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
private static final String QUERY_KEY = "QUERY_KEY";
private static final String QUERY_ORDER = "QUERY_ORDER";
// private static final String NI = "NI";
/**
* -- Called when the activity is first created
* ===================================================================
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
setContentView(R.layout.list_view2);
/**
* Populate the ActionBar----------------------------------
*/
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
getActionBar().setBackgroundDrawable(
getResources().getDrawable(R.drawable.actionbar_bg));
String sT = getIntent().getStringExtra("KEY_SUBTITLE");
actionBar.setSubtitle(sT);
/**
* Get the query string from last activity and pass it to this
* activity-----------------------------------------------------
*/
String q = null;
if (extras != null) {
q = extras.getString(QUERY_KEY);
}
loadQuery(q);
}
/**
* -- DB QUERY STUFF
* ===============================================================>
*
* Run the initial query from the previous activity---------------
*/
public void loadQuery(String q) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
String qO = getIntent().getStringExtra("QUERY_ORDER");
Cursor c = db.rawQuery(q + " ORDER BY `_id` " + qO, null);
setListAdapter(new QueryAdapter(this, c));
db.close();
} else {
Alerts.sdCardMissing(this);
}
}
/**
* The Query Adaptor --------------------------------------------
*/
private class QueryAdapter extends CursorAdapter {
public QueryAdapter(Context context, Cursor c) {
super(context, c);
LayoutInflater.from(context);
}
@Override
public void bindView(View v, Context context, final Cursor c) {
int tvLabel = c.getColumnIndexOrThrow("label");
String label = c.getString(tvLabel);
final TextView labelTxt = (TextView) v.findViewById(R.id.label);
if (labelTxt != null) {
labelTxt.setText("(" + label + ")");
}
int tvTitle = c.getColumnIndexOrThrow("title");
final String title = c.getString(tvTitle);
TextView titleTxt = (TextView) v.findViewById(R.id.listTitle);
if (titleTxt != null) {
titleTxt.setText(title);
}
int tvDescription = c.getColumnIndexOrThrow("description");
String description = c.getString(tvDescription);
TextView descriptionTxt = (TextView) v.findViewById(R.id.caption);
if (descriptionTxt != null) {
descriptionTxt.setText(description);
}
int tvDate = c.getColumnIndexOrThrow("date");
String date = c.getString(tvDate);
TextView dateTxt = (TextView) v.findViewById(R.id.dateAdded);
if (dateTxt != null) {
dateTxt.setText(date);
}
int tvGoto = c.getColumnIndexOrThrow("gotoURL");
final String gotoURL = c.getString(tvGoto);
TextView gotoTxt = (TextView) v.findViewById(R.id.dummy);
if (gotoTxt != null) {
gotoTxt.setText(gotoURL);
}
gotoTxt.setVisibility(View.GONE);
v.setTag(gotoURL);
final ListView lv = getListView();
lv.setEnabled(true);
lv.setClickable(true);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3) {
// -- Set the domain name in the strings.xml file once the
// DNS is established for the website.
String mDomain = getResources().getString(R.string.domain);
String url = "";
url = mDomain + (String) v.getTag();
int nI = c.getColumnIndexOrThrow("intent");
String intent = c.getString(nI);
Class<?> nIntent = null;
try {
nIntent = Class
.forName("com.myco.myapp.utili."
+ intent);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int tvTitle = c.getColumnIndexOrThrow("title");
String title = c.getString(tvTitle);
int tvLabel = c.getColumnIndexOrThrow("label");
String label = c.getString(tvLabel);
String queryKey = "SELECT * FROM " + label;
c.close();
db.close();
Intent i = new Intent(QueryDisplay.this, nIntent);
i.putExtra("QUERY_KEY", queryKey);
i.putExtra("KEY_URL", url);
Log.e("tag", url);
i.putExtra("KEY_SUBTITLE", title);
i.putExtra("KEY_LABEL", label);
i.putExtra("KEY_INTENT", intent);
i.putExtra("QUERY_ORDER", "ASC");
i.putExtra("KEY_YPOS", "0.0");
QueryDisplay.this.startActivity(i);
}
});
}
/**
* Inflate and returned the query to the view
* --------------------------------
*/
@Override
public View newView(Context context, Cursor c, ViewGroup parent) {
final View v = LayoutInflater.from(context).inflate(
R.layout.list_item, parent, false);
return v;
}
}
}
QueryDisplayFRAGMENT
上课:
public class QueryDisplayFRAGMENT extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;
@Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
****What do I do/put here to get the above list to show in this list fragment???
}
}
答案 0 :(得分:2)
尝试这样的事情:
public class QueryDisplayFRAGMENT extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;
protected TextView activityTitle;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File dbfile = new File(extStorageDirectory
+ "/myco/myapp/dB/myapp.db");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
private static final String QUERY_KEY = "QUERY_KEY";
private static final String QUERY_ORDER = "QUERY_ORDER";
private View layout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
layout = inflater.inflate(R.layout.list_view2, null);
return layout;
}
@Override
public void onActivityCreated(Bundle savedState) {
/**
* Populate the ActionBar----------------------------------
*/
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
getActionBar().setBackgroundDrawable(
getResources().getDrawable(R.drawable.actionbar_bg));
String sT = getIntent().getStringExtra("KEY_SUBTITLE");
actionBar.setSubtitle(sT);
/**
* Get the query string from last activity and pass it to this
* activity-----------------------------------------------------
*/
String q = null;
if (extras != null) {
q = extras.getString(QUERY_KEY);
}
loadQuery(q);
super.onActivityCreated(savedState);
}
public void loadQuery(String q) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
String qO = getIntent().getStringExtra("QUERY_ORDER");
Cursor c = db.rawQuery(q + " ORDER BY `_id` " + qO, null);
setListAdapter(new QueryAdapter(getActivity(), c));
db.close();
} else {
Alerts.sdCardMissing(getActivity());
}
}
}
如果您有问题,请告诉我。
答案 1 :(得分:0)
我在这里有一个类似的问题setCacheColorHint on listfragments,我解决了它让片段膨胀自定义布局而不是使用自己制作的...
如果您自己夸大布局,则可以访问该视图。希望这会有所帮助,
费德里科