0

我目前正在我的 android 应用程序上设计消息选项卡,并且我得到了消息活动以在列表视图中显示所有用户的消息。每个列表项都显示发送消息的人的姓名和消息的主题。在两个隐藏的文本字段中,我附加了发件人的 ID 和消息的 ID。正如我所见,它工作正常。但是,我正在尝试在长按后删除消息。但是发生的情况是,当我让后台进程运行时,它基本上将要删除的消息的 id 发送到服务器,我意识到对于每个列表项,我只发送第一项的消息 id。如果我单击任何其他项目,它仍会显示第一个消息 ID。我真的不知道我做错了什么。这就是我所做的。

public class MyMessages extends ListActivity {
    ArrayList<HashMap<String, String>> messages = new ArrayList<HashMap<String, String>>();
    JSONParser jsonParser=new JSONParser();
    public static String url_messages="...";
    public static String url_delete_message="...";
    public String theMessage;
    public String mid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_messages);

        new readMessages().execute();

          ListView lv= getListView();

            registerForContextMenu(lv);
            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    String message=((TextView) arg1.findViewById(R.id.rMessage)).getText().toString();
                    String name=((TextView) arg1.findViewById(R.id.sentName)).getText().toString();
                    String theirId=((TextView) arg1.findViewById(R.id.pid)).getText().toString();
                    Intent in= new Intent(getApplicationContext(), ReadMessage.class);
                    Bundle extras=new Bundle();
                    extras.putString("name",name);
                    extras.putString("message", message);
                    extras.putString("pid", theirId);
                    in.putExtras(extras);
                    //in.putExtra("picture", picture);
                    startActivityForResult(in, 100);

                }

            });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my_messages, menu);
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.my_messages, menu);

    }

    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.delete_message:
                // app icon in action bar clicked; go home
                //SaveSharedPreference.setUserName(TabExercise.this,"");
                new deleteMessage().execute();
               // Intent intent = new Intent(this, TabExercise.class);
               // intent.putExtra("tab_index","2");
               // intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
               // startActivity(intent);

                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    class readMessages extends AsyncTask<String, String, String>{

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generatNameValuePairb
            List<NameValuePair>param=new ArrayList<NameValuePair>();
            param.add(new BasicNameValuePair("id","4")); //SaveSharedPreference.getUserId(MyMessages.this)

            JSONObject json=jsonParser.makeHttpRequest(url_messages, "POST", param);
            Log.d("My messages", json.toString());

            try {

                JSONArray array = json.getJSONArray("messages");
                Log.d("Messages length",Integer.toString(array.length()));
                //if(array.length()==0)
                    //Log.d("Messages?","No Messages");
                //if(array.length()>0){
                for (int i = 0; i < array.length(); i++) {
                   // Log.i("name", array.getJSONObject(i).getString("name"));
                    JSONObject c = array.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString("sender_id");
                    String name = c.getString("sender_name");
                    String subject=c.getString("subject");
                    theMessage=c.getString("message");
                    mid=c.getString("message_id");
                    //Log.i("Message Ids", mid);
                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put("contact_id", id);
                    map.put("sender_name", name);
                    map.put("subject", subject);
                    map.put("message", theMessage);
                    map.put("message_id", mid);

                    // adding HashList to ArrayList
                    messages.add(map);
                }
            /*  }else{
                     runOnUiThread(new Runnable() {
                            public void run() {
                    TextView tx=(TextView)findViewById(R.id.nocontacts);
                    tx.setVisibility(0);
                            }
                     });

                }*/
            }catch (Exception e) {
                e.printStackTrace();
            }
                return null;
            }
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products

            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {

                    ListAdapter adapter = new SimpleAdapter(
                            MyMessages.this, messages,
                            R.layout.list_all_messages, new String[] { "contact_id","sender_name", "subject", "message", "message_id"},
                            new int[] { R.id.pid, R.id.sentName, R.id.recMessage, R.id.rMessage, R.id.mid });
                    // updating listview
                    setListAdapter(adapter);

                }
            });

        }

    }
    class deleteMessage extends AsyncTask<String, String, String>{
        String theirName=((TextView) findViewById(R.id.sentName)).getText().toString();
        String messageId=((TextView) findViewById(R.id.mid)).getText().toString();
        String theirId=((TextView) findViewById(R.id.pid)).getText().toString();
        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generatNameValuePairb
            //List<NameValuePair>param=new ArrayList<NameValuePair>();
            //param.add(new BasicNameValuePair("id","4")); //SaveSharedPreference.getUserId(MyMessages.this)
            //param.add(new BasicNameValuePair("mid",messageId));
            //JSONObject json=jsonParser.makeHttpRequest(url_delete_message, "POST", param);
            Log.d("Message Id", messageId );
            Log.d("Their name", theirName );
            Log.d("Their Id", theirId );
            //Log.d("Message status", json.toString());


                return null;
            }
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products

            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
            //new readNotifications().execute();
                }
            });

        }

    }
}

这是我用来获取 AsyncTask 中删除时每条消息的 id 的语句

String messageId=((TextView) findViewById(R.id.mid)).getText().toString();

我觉得,这可能是问题所在,因为它可能只返回第一个列表项的 id。

4

1 回答 1

0

您对问题出在哪里是正确的,并且是预期的行为来获得此代码的第一项。

在文档中您可以阅读有关如何在列表视图上实现上下文菜单的信息

简而言之,要获取所选列表项的 ID,您必须调用方法

AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

onContextItemSelected(MenuItem item)方法内部

info.id

包含您可以传递给您的异步任务的选定项目的 ID。

于 2013-07-19T01:32:49.590 回答