2016-03-01 43 views
1

我已經創建了一個自定義alertbar的上下文菜單,這是從json解析查看列表。我想要的是獲取詳細信息(打開上下文菜單的特定人員的姓名)。我想在警報欄上設置文本。 非常感謝您的幫助。如何從adapterclass中設置文本()

enter code here 

公共類MyConnectionAsyncTask延伸的AsyncTask> {

@Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     progressDialog = new ProgressDialog(getActivity()); 
     progressDialog.setTitle("Connecting..."); 
     progressDialog.show(); 
    } 

    @Override 
    protected List<MyconnectionManager> doInBackground(String... params) { 

     try { 
      URL url = new URL(params[0]);// taking params from execute method 
      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
      httpURLConnection.connect();//opening connection 
      InputStream inputStream = httpURLConnection.getInputStream(); 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));//putting data into buffer from input stream 

      String result = ""; 
      String line = ""; 
      while ((line = bufferedReader.readLine()) != null) { 
       result += line; 

      } 
      String json_str = result.toString(); 

      JSONObject parentObject = new JSONObject(json_str); 
      JSONArray parentArray = parentObject.getJSONArray("result_array");//ARRAY name from API will come here 

      ArrayList<MyconnectionManager> connectionManagerList = new ArrayList<MyconnectionManager>();//created a list bc we have list of connections 

      for (int i = 0; i < parentArray.length(); i++) { 
       JSONObject finalObject = parentArray.getJSONObject(i); 
       MyconnectionManager myconnectionManager = new MyconnectionManager(getActivity()); 
       myconnectionManager.setFullname_myConnection(finalObject.getString("fullname")); 
       myconnectionManager.setProfilePic_myConnection(finalObject.getString("profile_pic")); 
       myconnectionManager.setPosition_myconnection(finalObject.getString("position")); 
       myconnectionManager.setCompany_myConnection(finalObject.getString("company")); 
       myconnectionManager.setLocation_myConnection(finalObject.getString("location")); 
       connectionManagerList.add(myconnectionManager);//adding to list 

      } 
      return connectionManagerList; 


     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    @Override 
    protected void onPostExecute(List<MyconnectionManager> result) { 
     super.onPostExecute(result); 
     progressDialog.dismiss(); 
     MyConnectionAdapter adapter = new MyConnectionAdapter(getActivity(), R.layout.single_row_my_connections, result);//creating adpater here 
     connectionList.setAdapter(adapter); 
    } 
} 

adpater類如下

public class MyConnectionAdapter extends ArrayAdapter { 

    private List<MyconnectionManager> myconnectionManagerList; 
    private int resource; 
    LayoutInflater inflater; 

    public MyConnectionAdapter(Context context, int resource, List objects) { 
     super(context, resource, objects); 
     this.resource = resource;// resource has the layout view so we can also give resource below instead of layout 
     myconnectionManagerList = objects; 
     inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

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

     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.single_row_my_connections, null);//we can give resource here 
     } 
     ImageView imageView; 
     TextView name, company, location, position_company; 

     imageView = (ImageView) convertView.findViewById(R.id.iv_profile_pic_myconnection); 
     name = (TextView) convertView.findViewById(R.id.tvname_myconncetion); 
     company = (TextView) convertView.findViewById(R.id.tvCompany_myconnection); 
     location = (TextView) convertView.findViewById(R.id.tvlocation_myconnections); 
     position_company = (TextView) convertView.findViewById(R.id.tvPosition_myconnection); 

     ImageLoader.getInstance().displayImage(myconnectionManagerList.get(position).getProfilePic_myConnection(), imageView); 

     name.setText(myconnectionManagerList.get(position).getFullname_myConnection()); 
     company.setText(myconnectionManagerList.get(position).getCompany_myConnection()); 
     location.setText(myconnectionManagerList.get(position).getLocation_myConnection()); 
     position_company.setText(myconnectionManagerList.get(position).getPosition_myconnection()); 
     return convertView; 
    } 

} 

上下文菜單低於

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 

    menu.setHeaderIcon(R.drawable.small_logo); 
    menu.setHeaderTitle("Choose Action"); 
    menu.add(0, 1, 0, "Send Message"); 
    menu.add(0,2,1,"Remove Connection"); 
} 

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    if (item.getItemId()==1){ 
     Toast.makeText(getActivity(),"Send Message",Toast.LENGTH_SHORT).show(); 
     View view=LayoutInflater.from(getActivity()).inflate(R.layout.send_message_layout,null,false); 
     final EditText et_sub= (EditText) view.findViewById(R.id.et_sub_send_Message); 
     final EditText et_msg= (EditText) view.findViewById(R.id.etMessage_send_Message); 
     TextView name= (TextView) view.findViewById(R.id.tv_name_send_message); 




     name.setText("To: ");//name of user will come here 

     AlertDialog.Builder ab=new AlertDialog.Builder(getActivity()); 
     ab.setTitle("Send Message"); 
     ab.setView(view); 
     ab.setNegativeButton("Cancel", null); 
     ab.setPositiveButton("Send", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       String sub = et_sub.getText().toString(); 
       String msg = et_msg.getText().toString(); 
       Toast.makeText(getActivity(), sub + " " + msg, Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     ab.show(); 


    }else { 
     Toast.makeText(getActivity(),"Remove Connection",Toast.LENGTH_SHORT).show(); 
    } 
    return true; 
} 
+0

兄弟我修改了代碼,請嘗試,讓我知道結果 – saeed

回答

0

您可以使用

@Override 
     public boolean onContextItemSelected(MenuItem item) { 
      AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
      int listPosition = info.position; 
      String nameoOfUser = ((TextView)info.targetView.findViewById(R.id.YOUR_TEXTVIEW_ID)).getText().toString(); 


    name.setText("To: " + nameoOfUser); 
    } 
+0

我想要的是從列表中獲取名稱,這是在json解析後的活動中顯示的。謝謝你:-) –

+0

它看起來會起作用,你可以寫下其餘的代碼。 –

+0

試圖做到這一點。沒有得到結果。 –

-1
TextView textViewItem = (TextView)convertView.findViewById(R.id.textViewItem); 
    textViewItem.setText("your text"); 
+0

name.setText(「爲:」); //這是我想要的用戶名稱 –

相關問題