0

我正在开发一个应用程序,并且我已经实现了 sherlockListActvity 来显示列表。但是 onItemClickListener 并没有解决这个问题。我尝试在互联网上搜索,但没有一个对我有用。我做了我在互联网上找到的一切来解决这个问题。请帮助我。我是安卓新手。代码是这样的。

新闻活动.java

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.ministry.ensing119app.Contact;
import com.ministry.ensing119app.HomeScreen;
import com.ministry.ensing119app.R;
import com.ministry.ensing119app.bible.BibleActivity;
import com.ministry.ensing119app.donate.Donate;
import com.ministry.ensing119app.photos.GetPath;
import com.ministry.ensing119app.sermonnotes.Notes_list;

public class NewsActivity extends SherlockListActivity {

public static String url = "http://ensignweb.com/sandbox/app/comment11.php";

    // JSON Node names
    protected static final String TAG_PRODUCTS = "products";
    protected static final String TAG_CID = "cid";
    public static final String TAG_NAME = "name";
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

    // contacts JSONArray  
    JSONArray products = null;
    ActionBar actionBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        actionBar = getSupportActionBar();
        setContentView(R.layout.activity_news);
        ConnectivityManager connectivityManager 
        = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
            Log.d("if condition", "if condition is running");
            new Message().execute(url);


//           The service section
            Intent intent = new Intent(this,UpdateService.class);
            PendingIntent pIntent = PendingIntent.getService(this, 0, intent, 0);
            AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30000, pIntent);
            startService(new Intent(this, UpdateService.class));
        } else {
            Log.d("if condition", "else condition is running");
        Toast.makeText(getApplicationContext(), "There is no internet or low. Please check", Toast.LENGTH_LONG).show();
        Intent returnIntent = new Intent(NewsActivity.this, HomeScreen.class);
        startActivity(returnIntent);
        }
        ListView list = getListView();
        list.setClickable(true);
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                TextView clickedData = (TextView) ((TextView) arg1).getText();
                Toast.makeText(getApplicationContext(),
                        clickedData.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.news, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        // TODO Auto-generated method stub
        switch(item.getItemId()){
        case R.id.donate:
            Intent newsIntent = new Intent(NewsActivity.this, Donate.class);
            startActivity(newsIntent);
            break;

        case R.id.photos:
            Intent photoIntent = new Intent(NewsActivity.this, GetPath.class);
            startActivity(photoIntent);
            break;

        case R.id.notepad:
            Intent notePadIntent = new Intent(NewsActivity.this, Notes_list.class);
            startActivity(notePadIntent);
            break;

        case R.id.contact:
            Intent contactIntent = new Intent(NewsActivity.this,Contact.class);
            startActivity(contactIntent);
            break;

        case R.id.bible:
            Intent BibleIntent = new Intent(NewsActivity.this, BibleActivity.class);
            startActivity(BibleIntent);
            break;
        }
        return super.onMenuItemSelected(featureId, item);
    }

    class Message extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>> > {

        ListView lv = (ListView) findViewById(android.R.id.list);
        ProgressDialog progress = new ProgressDialog(NewsActivity.this);
        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();
                @Override
            protected ArrayList<HashMap<String, String>> doInBackground(String... params) {

                    Log.d("doInBackgound","backgound is running");

                    // getting JSON string from URL
                    JSONObject json = jParser.getJSONFromUrl(url);

                    Log.d("path_parsing", "before parsing");
                    try {
                        // Getting Array of Contacts
                        products = json.getJSONArray(TAG_PRODUCTS);

                        // looping through All Contacts
                        for(int i = products.length()-1; i >=0; i--){
                            JSONObject c = products.getJSONObject(i);

                            // Storing each json item in variable
                            String cid = c.getString(TAG_CID).toString();
                            String name = c.getString(TAG_NAME);
                            // creating new HashMap
                            HashMap<String, String> map = new HashMap<String, String>();

                            // adding each child node to HashMap key => value
                            map.put(TAG_CID, cid);
                            map.put(TAG_NAME, name);

                            // adding HashList to ArrayList
                            mylist.add(map);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Log.d("path_parsing", "after parsing");
                    return mylist;

                }

            @Override
            protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
                if(progress.isShowing()){
                    progress.dismiss();
                }
                ListAdapter adapter = new SimpleAdapter(NewsActivity.this, result , R.layout.list_item,new String[] { TAG_NAME,}, new int[] {
                        R.id.name});
                lv.setAdapter(adapter);
                lv.setTextFilterEnabled(true);
                Log.d("postExecute","Postexecute is running");
                lv.setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        // TODO Auto-generated method stub

                        Log.d("selected", String.valueOf(arg2).toString());
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub

                    }
                });
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                // TODO Auto-generated method stub
                super.onProgressUpdate(values);
            }

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                progress.setTitle("Progress");
                progress.setMessage("Please have patience");
                progress.show();
            }
      }
}
4

1 回答 1

0

尝试这个

public class NewsActivity extends SherlockListActivity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_news);
       ListView list = getListView();
       list.setOnItemClickListener(new OnItemClickListener() 
       {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
            {
                Toast.makeText(this, "test click position: " + arg2, Toast.LENGTH_SHORT).show();
            }
        });
        list.setAdapter(YOUR ADAPTER)
    }

}

如果您没有看到消息,请发布您的 xml (activity_news.xml)

于 2013-05-27T11:25:29.580 回答