如何从adapterclass setText()

时间:2016-03-01 11:09:06

标签: android arrays json

我在上下文菜单上创建了一个自定义警报栏,该菜单位于从json解析查看的列表中。我想要的是获取详细信息(打开上下文菜单的特定人员的姓名)。我想在警报栏上设置文本。 非常感谢您的帮助。

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 Class在

之下
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;
}

2 个答案:

答案 0 :(得分: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 );
    }

答案 1 :(得分:-1)

TextView textViewItem = (TextView)convertView.findViewById(R.id.textViewItem);
    textViewItem.setText("your text");