【问题标题】:Android expandableListView add variable to a buttonAndroid expandableListView将变量添加到按钮
【发布时间】:2018-03-09 05:31:27
【问题描述】:

希望您能提供帮助。 我需要向一个按钮添加一个变量,我根据数据库中可能存在或不存在的值使其可见。这工作正常。 但我现在需要将 id 添加到此按钮。 请看下文。

ExpandableListView.java

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    final TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);

    //listDataChild
    final Button markAsBoughtButton = convertView.findViewById(R.id.BtnToClick);

    // set 'Mark as bought' button to visible
    if(childText.contains("bought by")){
        markAsBoughtButton.setVisibility(View.GONE);
    }else{
        markAsBoughtButton.setVisibility(View.VISIBLE);
    }

    markAsBoughtButton.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick(View view) {
             View parentRow = (View) view.getParent();
             ListView listView = (ListView) parentRow.getParent();
             final int pos = listView.getPositionForView(parentRow);

             Toast.makeText(_context, "Bought! " + " Position " + pos, Toast.LENGTH_LONG).show();
         }
    });

    txtListChild.setText(childText);
    return convertView;
}

等等

MainActivity.java

public class MainActivity extends ProfileActivity {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;

private ProgressDialog mprocessingdialog;
private static String url = "http://www.xxx/xxxx/xxxx/xxxx.php";
public static String id;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // added to include custom icon
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setIcon(ic_launcher);

    mprocessingdialog = new ProgressDialog(this);

    // get the listview
    expListView = (ExpandableListView) findViewById(R.id.lvExp);

    // preparing list data
    new DownloadJason().execute();
}

/*
 * Preparing the list data
 */
private class DownloadJason extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        //Showing Progress dialog
        mprocessingdialog.setTitle("Please Wait..");
        mprocessingdialog.setMessage("Loading");
        mprocessingdialog.setCancelable(false);
        mprocessingdialog.setIndeterminate(false);
        mprocessingdialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub

        JSONParser jp = new JSONParser();
        String jsonstr = jp.makeServiceCall(url);
        Log.d("Response = ", jsonstr);

        if (jsonstr != null) {
            // For Header title Arraylist
            listDataHeader = new ArrayList<String>();

            // Hashmap for child data key = header title and value = Arraylist (child data)
            listDataChild = new HashMap<String, List<String>>();

            try {
                JSONObject jobj = new JSONObject(jsonstr);
                JSONArray jarray = jobj.getJSONArray("presents_db");

                for (int hk = 0; hk < jarray.length(); hk++) {
                    JSONObject d = jarray.getJSONObject(hk);

                    // Adding Header data
                    listDataHeader.add(d.getString("name") + "'s presents are: ");

                    // Adding child data for members
                    List<String> members = new ArrayList<String>();

                    JSONArray xarray = d.getJSONArray("presents_list");
                    for (int i = 0; i < xarray.length(); i++) {
                        JSONObject a = xarray.getJSONObject(i);
                        String id = a.getString("id");
                        String present = a.getString("present");

                        if("".equals(a.getString("bought_by"))) {
                            members.add(id + " " + present);
                        }else{
                            members.add(id + " " + present + "\r\n bought by: " + a.getString("bought_by"));
                        }
                    }

                    // Header into Child data
                    listDataChild.put(listDataHeader.get(hk), members);
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            Toast.makeText(getApplicationContext(),"Please Check internet Connection", Toast.LENGTH_SHORT).show();
        }
        return null;
    }

对糟糕的编码表示歉意,但我是 Java 新手,学习曲线陡峭。 感谢您的帮助!

【问题讨论】:

    标签: android json variables button expandablelistview


    【解决方案1】:

    您可以使用setTag()getTag()

    //Set tag
    button.setTag(position);
    
    //Get Tag 
    new OnClickListener() {
    @Override
    public void onClick(View v) {
        int pos = v.getTag(); 
    //Get Tag work on view like button
    }
    

    What is the main purpose of setTag() getTag() methods of View?

    【讨论】:

    • 感谢 Upendra,但我已经尝试过了。不确定在哪里插入代码,我需要传递 id 而不是位置。
    • 在你的适配器中使用 setTag() 并在你想要的地方使用 getTag() 。是的,您还可以添加 id 或任何您想要的内容
    • 嗨乌彭德拉。对不起,但这不起作用。这是错误的方式。我需要从 MainActivity 获取产品的 id(来自 json)到 ExpandableListAdapter 而不是相反。还是我错过了什么……?
    • 对不起,我没有得到你想要的。你能解释一下吗?
    • 我需要的是,按下按钮来更新数据库。所以, "if(childText.contains("bought by")){ markAsBoughtButton.setVisibility(View.GONE); }else{ markAsBoughtButton.setVisibility(View.VISIBLE); }" 使按钮可见,但我还需要以某种方式附上 id,这样我就可以用它更新数据库。现在有意义吗?我对 php 和 javascript 非常熟悉,但这一切都非常不同,也许我从错误的角度接近它。
    猜你喜欢
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 2015-06-26
    • 1970-01-01
    • 2012-09-11
    相关资源
    最近更新 更多