我是android的新手,我不太了解适配器的使用?我一直在尝试制作 ContextMenu,它会在单击列表中的一个项目时弹出:我的 Activity 就像这样,没有我尝试过的 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;
}
}
我想要做的是打开一个包含两个动作的上下文菜单:Action1 和 Action2 ...