2012-07-09 63 views
1

我是一個Android的新手,我真的不明白使用適配器?我一直試圖讓文本菜單,將彈出的點擊從列表中選擇項:我的活動是這樣的沒有什麼,我試過了文本菜單,因爲它是有用的:Android適配器和ContextMenu

public class Proj2Activity extends Activity { 
public static final String TAG = "MyActivity"; 
private TweetListAdapter1 twa; 
private ListView myListView; 
ArrayList<TweetModel> tweets; 
public static double distance(float lat_a, float lng_a, float lat_b, float lng_b) { 
    float pk = (float) (180/3.14169); 

    float a1 = lat_a/pk; 
    float a2 = lng_a/pk; 
    float b1 = lat_b/pk; 
    float b2 = lng_b/pk; 

    float t1 = FloatMath.cos(a1)*FloatMath.cos(a2)*FloatMath.cos(b1)*FloatMath.cos(b2); 
    float t2 = FloatMath.cos(a1)*FloatMath.sin(a2)*FloatMath.cos(b1)*FloatMath.sin(b2); 
    float t3 = FloatMath.sin(a1)*FloatMath.sin(b1); 
    double tt = Math.acos(t1 + t2 + t3); 

    return 6366000*tt; 
} 


public static Location getLocation(Context ctx) { 
    LocationManager lm = (LocationManager) ctx 
      .getSystemService(Context.LOCATION_SERVICE); 
    List<String> providers = lm.getProviders(true); 

    /* 
    * Loop over the array backwards, and if you get an accurate location, 
    * then break out the loop 
    */ 
    Location l = null; 

    for (int i = providers.size() - 1; i >= 0; i--) { 
     l = lm.getLastKnownLocation(providers.get(i)); 
     if (l != null) 
      break; 
    } 
    return l; 
} 




/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    LoadTweetsTask ltt = new LoadTweetsTask("androiddev"); 
    ltt.execute(); 


    final TextView text1 = (TextView) findViewById(R.id.nume); 
    final Button btnGo = (Button) findViewById(R.id.button1); 

    btnGo.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      String name1 = text1.getText().toString(); 

      SharedPreferences settings = getSharedPreferences("My_Pref", 0); 
       SharedPreferences.Editor editor = settings.edit(); 
       editor.putBoolean(name1, true); 
       editor.commit(); 
      LoadTweetsTask ltt = new LoadTweetsTask(name1); 
      ltt.execute(); 
     } 
    }); 


} 




private class LoadTweetsTask extends AsyncTask<Void, Void, Void> { 

    ProgressDialog progressDialog; 
    public String name; 


    public LoadTweetsTask(String name) { 
     this.name = name; 
    } 

    @Override 
    protected void onPreExecute() { 
     tweets = new ArrayList<TweetModel>(); 

     progressDialog = new ProgressDialog(Proj2Activity.this); 
     progressDialog.setMessage("Loading Tweets"); 
     progressDialog.show(); 

    } 

    @Override 
    protected void onPostExecute(Void result) { 
     progressDialog.dismiss(); 

     TweetListAdapter1 adapter = new TweetListAdapter1(getApplicationContext(), tweets); 
     ((ListView)findViewById(R.id.listTweets)).setAdapter(adapter); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 



     HttpClient client = new DefaultHttpClient(); 

     String requestUrl = "http://api.twitter.com/1/statuses/user_timeline.json" + "?screen_name=" + name + "&count=200"; 
     HttpGet request = new HttpGet(requestUrl); 
     ResponseHandler<String> responseHandler = new BasicResponseHandler(); 

     try { 
      String jsonResponse = client.execute(request, responseHandler); 

      JSONArray json = new JSONArray(jsonResponse); 
      //used to display 20 valid tweets from 200 tweets which have the location specified 
      int count = 0; 
      for (int i = 0; i < json.length(); i++) { 
       if(count <= 20){ 
        JSONObject jsonTweet = json.getJSONObject(i); 

        try{ 

         JSONObject obj = jsonTweet.getJSONObject("geo"); 

         if(obj!=null){ 
          Location myLocation = getLocation(getBaseContext()); 
          double distance; 
          JSONArray arr = obj.getJSONArray("coordinates"); 
          float tweetc1 = (float) arr.getDouble(0); 
          float tweetc2 = (float) arr.getDouble(1); 
          Log.d("COORDTweet",Float.toString(tweetc1)+ " " +Float.toString(tweetc2)); 
          float my1 = (float) 44.438254; 
          float my2 = (float) 26.051009; 
          Log.d("COORDMY",Float.toString(my1)+ " " +Float.toString(my2)); 
          distance = distance(tweetc1, tweetc2, my1, my2); 
          Log.d("Distance",Double.toString(distance)); 
          tweets.add(new TweetModel(jsonTweet.getJSONObject("user").getString("name"), jsonTweet.getString("created_at"), jsonTweet.getString("text"),(int)distance/1000)); 

          count++; 
         } 

         Log.d("TRY", "OK"); 

        }catch(JSONException e){ 
         Log.d("EXCP","NULL"); 
        } 

       } 
       else{ 
        break; 
       } 
      } 

     } 

     catch(ClientProtocolException e) { 
      Log.e(TAG, e.getMessage(), e); 
     } 
     catch (IOException e) { 
      Log.e(TAG, e.getMessage(), e); 
     } 
     catch (JSONException e) { 
      Log.e(TAG, e.getMessage(), e); 
     } 
     return null; 

    } 

} 
} 

適配器類:

public class TweetListAdapter1 extends BaseAdapter { 
Context context; 
ArrayList<TweetModel> items; 

public TweetListAdapter1(Context context, ArrayList<TweetModel> items) { 
    this.context = context; 
    this.items = items; 
} 

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

public Object getItem(int arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

public long getItemId(int arg0) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 

    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(R.layout.list_item_tweet, parent, false); 

    TextView txtMessage = (TextView)rowView.findViewById(R.id.txtMessage); 
    TweetModel m = items.get(position); 
    txtMessage.setText("Name: " + m.getName() +"\n"+ "Distance :"+ m.getDistance() + " Km\n" + "Tweet: " + m.getTweet() + "\n" + "Date: " + m.getDate() + "\n"); 

    TextView txtMessage1 = (TextView)rowView.findViewById(R.id.txtMessage1); 
    txtMessage1.setText("Ala bala "); 
    return rowView; 
} 

} 

我想要做的就是打開上下文菜單有兩個動作:措施1和措施2 ...

回答

1

我一直試圖讓文本菜單,將彈出的點擊在 ai從列表中的項目

所以你需要在你的Activity類中實現它。

首先,你需要重寫onCreateContextMenu,然後又重寫onContextItemSelected方法

@Override 
    public void onCreateContextMenu(ContextMenu cMenu, View parent, ContextMenu.ContextMenuInfo info) { 
     new MenuInflater(YourActivity.this).inflate(R.menu.conmenu, cMenu); 
    } 

@Override 
    public boolean onContextItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.option1: 
       //do some work 
         break; 
         case R.id.option2: 
           // do some work 
         break; 
      } 
     ... 
    } 

您需要閱讀一些教程這樣Android: Context menu exampleContext Menu | Android Developer Tutorial

+0

我不明白您的回答,您的信息與相關活動不帶適配器,請重新閱讀後! – delive 2015-10-28 23:26:01