我无法构建一个正确的问题,但这就是我所面对的问题。我有一个ListActvity
,其中包含以下代码:
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class dbMainactivty extends ListActivity {
// Declare Variables
public static final String ROW_ID = "row_id";
private static final String TITLE = "title";
private ListView noteListView;
private CursorAdapter noteAdapter;
private TextView text;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Tracker t = ((AnalyticsSampleApp)this.getApplication()).getTracker(TrackerName.APP_TRACKER);
t.setScreenName("dbMainactivty");
t.send(new HitBuilders.AppViewBuilder().build());
// Locate ListView
noteListView = getListView();
// setContentView(R.layout.list_note);
//noteListView = (ListView) findViewById(R.id.listview);
setContentView(R.layout.dbmainactivitylayout);
text = (TextView) findViewById(R.id.mainText);
// Prepare ListView Item Click Listener
noteListView.setOnItemClickListener(viewNoteListener);
// Map all the titles into the ViewTitleNotes TextView
String[] from = new String[] { TITLE };
int[] to = new int[] { R.id.ViewTitleNotes };
// Create a SimpleCursorAdapter
noteAdapter = new SimpleCursorAdapter(dbMainactivty.this,
R.layout.list_note, null, from, to);
// Set the Adapter into SimpleCursorAdapter
setListAdapter(noteAdapter);
}
// Capture ListView item click
OnItemClickListener viewNoteListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// Open ViewNote activity
Intent viewnote = new Intent(dbMainactivty.this, ViewNote.class);
// Pass the ROW_ID to ViewNote activity
viewnote.putExtra(ROW_ID, arg3);
startActivity(viewnote);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
};
@Override
protected void onResume() {
super.onResume();
// Execute GetNotes Asynctask on return to MainActivity
new GetNotes().execute((Object[]) null);
GoogleAnalytics.getInstance(dbMainactivty.this).reportActivityStart(this);
}
@Override
protected void onStop() {
Cursor cursor = noteAdapter.getCursor();
// Deactivates the Cursor
if (cursor != null)
cursor.deactivate();
noteAdapter.changeCursor(null);
super.onStop();
GoogleAnalytics.getInstance(dbMainactivty.this).reportActivityStop(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
//return true;
return super.onCreateOptionsMenu(menu);
}
//@Override
//public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
//
// if (drawerListener.onOptionsItemSelected(item)) {
// return true;
// }
Intent i = null;
switch (item.getItemId()) {
case R.id.action_rate:
// Intent ia = new Intent(Intent.ACTION_VIEW);
// ia.setData(Uri.parse("market://details?id=" + getApplicationContext().getPackageName()));
// startActivity(i);
//break;
String webpage = "http://developer.android.com/index.html";
Intent intent2 = new Intent(Intent.ACTION_VIEW, Uri.parse(webpage));
startActivity(intent2);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
Tracker t = ((AnalyticsSampleApp)this.getApplication()).getTracker(TrackerName.APP_TRACKER);
t.send(new HitBuilders.EventBuilder()
.setCategory("rate CategoryListActivity") // category i.e. Player Buttons
.setAction("Button") // action i.e. Play
.setLabel("clicked") // label i.e. any meta-data
.build());
case R.id.action_share:
i = new Intent();
i.setAction(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_TEXT, "");
i.setType("text/plain");
startActivity(i);
Tracker t1 = ((AnalyticsSampleApp)this.getApplication()).getTracker(TrackerName.APP_TRACKER);
t1.send(new HitBuilders.EventBuilder()
.setCategory("share CategoryListActivity") // category i.e. Player Buttons
.setAction("Button") // action i.e. Play
.setLabel("clicked") // label i.e. any meta-data
.build());
return true;
case R.id.action_maila:
Intent imail = new Intent(Intent.ACTION_SEND);
imail.setType("message/rfc822");
imail.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient@example.com"});
imail.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
imail.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
startActivity(Intent.createChooser(imail, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(dbMainactivty.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
Tracker t2 = ((AnalyticsSampleApp)this.getApplication()).getTracker(TrackerName.APP_TRACKER);
t2.send(new HitBuilders.EventBuilder()
.setCategory("Mail CategoryListActivity") // category i.e. Player Buttons
.setAction("Button") // action i.e. Play
.setLabel("clicked") // label i.e. any meta-data
.build());
break;
case R.id.action_home:
Intent ihome = new Intent(this, MainActivity.class);
startActivity(ihome);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
Tracker t3 = ((AnalyticsSampleApp)this.getApplication()).getTracker(TrackerName.APP_TRACKER);
t3.send(new HitBuilders.EventBuilder()
.setCategory("GoToHome CategoryListActivity") // category i.e. Player Buttons
.setAction("Button") // action i.e. Play
.setLabel("clicked") // label i.e. any meta-data
.build());
return true;
}
return super.onOptionsItemSelected(item);
};
// GetNotes AsyncTask
private class GetNotes extends AsyncTask<Object, Object, Cursor> {
DatabaseConnector dbConnector = new DatabaseConnector(dbMainactivty.this);
@Override
protected Cursor doInBackground(Object... params) {
// Open the database
dbConnector.open();
return dbConnector.ListAllNotes("maincat LIKE 'quiz' AND subcat LIKE 'test'");
}
@Override
protected void onPostExecute(Cursor result) {
noteAdapter.changeCursor(result);
// Close Database
Cursor cursor = noteAdapter.getCursor();
if(cursor != null && cursor.getCount() > 0){
cursor.moveToFirst();
//do your action
//Fetch your data
// GoogleAnalytics.getInstance(dbMainactivty.this).reportActivityStart(this);
//Toast.makeText(getBaseContext(), "Yipeee!", Toast.LENGTH_SHORT).show();
}
else {
//Toast.makeText(getBaseContext(), "No records yet!", Toast.LENGTH_SHORT).show();
Toast toast = Toast.makeText(dbMainactivty.this, "No records yet!", Toast.LENGTH_SHORT);
toast.show();
toast.setGravity(Gravity.CENTER, 0, 0);
finish();
}
dbConnector.close();
}
}
@Override
protected void onStart() {
super.onStart();
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(
"Please check your Internet Connection.")
.setTitle("")
.setCancelable(false)
.setPositiveButton("Exit",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
//loader.cancel(true);
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
} else {GoogleAnalytics.getInstance(dbMainactivty.this).reportActivityStart(this);
}
}
}
现在布局文件为list_note.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.96"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/ViewTitleNotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:textColor="#000000"
android:textStyle="bold"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
当我实现上面的代码时,每件事情都可以。但是,我想要包含一些其他数据,所以我调用另一个包含其他数据的布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/mainText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pinned Questions" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/mainText"
android:background="#FFFFFF"
/>
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/mainText"
android:text="There is no data"
android:textStyle="bold" />
</RelativeLayout>
当我包含此布局时,列表会显示但单击列表项时没有任何反应。正常工作是当我点击列表项时,它应该用所需的点击项打开下一个活动。
提前致谢。
答案 0 :(得分:0)
我建议您Implement
View.OnItemClickListener()
,然后使用
noteListView.setOnItemClickListener(this);
这将完美地运作。 希望我帮忙。