如何点击自定义列表视图中的号码添加到动作栏中的机器人自定义、视图、机器人、栏中

2023-09-03 21:47:02 作者:゛冷瞳°

请帮助我如何在Android中点击添加到自定义操作杆(TextView中)的自定义列表视图的数量?

下面是我的活动

 公共类DetaisRESTActivity延伸活动{

字符串valueid,valueid1,VALUENAME;
公众诠释计数= 0;
的JSONObject的JSONObject;
JSONArray jsonarray;
公众诠释listViewClickCounter = 0;

ListAdapterAddItems适配器;

字符串restaurantmenuname,rastaurantname;

ProgressDialog mProgressDialog;
ArrayList的< ListModel的> ArrayList的;
公共静态字符串RASTAURANTNAMEDETAILS =RestaurantPizzaItemName;
公共静态字符串RASTAURANTRUPPEES =RestaurantPizzaItemPrice;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
   // requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    的setContentView(R.layout.activity_detais_rest);
 // getWindow()setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar);
    动作条动作条= getActionBar();

    actionBar.setDisplayHomeAsUpEnabled(真正的);
    actionBar.setBackgroundDrawable(新ColorDrawable(Color.parseColor(#FF0000)));

    LayoutInflater mInflater = LayoutInflater.from(本);

    查看mCustomView = mInflater.inflate(R.layout.titlebar,NULL);
   TextView的mTitleTextView =(TextView中)mCustomView.findViewById(R.id.textView123456789);
    Toast.makeText(DetaisRESTActivity.this,listViewClickCounter,Toast.LENGTH_LONG).show();
    mTitleTextView.setText(listViewClickCounter);

    的ImageButton的ImageButton =(的ImageButton)mCustomView
            .findViewById(R.id.imageButton2);
    imageButton.setOnClickListener(新View.OnClickListener(){


        @覆盖
        公共无效的onClick(视图查看){
            Toast.makeText(getApplicationContext()刷新点击过!
                    Toast.LENGTH_LONG).show();
            意图I =新的意图(DetaisRESTActivity.this,QuentityActivity.class);
            startActivity(ⅰ);
        }
    });

    actionBar.setCustomView(mCustomView);
    actionBar.setDisplayShowCustomEnabled(真正的);
    意向意图= getIntent();
    //获取额外价值
    值名= intent.getStringExtra(restaurantmenuname);
    valueid = intent.getStringExtra(restaurantmenunameid);
    valueid1 = intent.getStringExtra(idsrestaurantMenuId5);

    Log.i(valueid,valueid);
    Log.i(VALUENAME,VALUENAME);
    Log.i(valueid1,valueid1);

    新DownloadJSON()执行();
}


// DownloadJSON的AsyncTask
私有类DownloadJSON扩展的AsyncTask<虚空,虚空,虚空> {

    @覆盖

    在preExecute保护无效(){
        super.on preExecute();
        //创建一个progressdialog
        mProgressDialog =新ProgressDialog(DetaisRESTActivity.this);
        //设置progressdialog称号
        mProgressDialog.setTitle(Android的JSON解析教程);
        //设置progressdialog消息
        mProgressDialog.setMessage(载入中...);
        mProgressDialog.setIndeterminate(假);
        //显示progressdialog
        mProgressDialog.show();
        Toast.makeText(DetaisRESTActivity.thisSuccesss,Toast.LENGTH_LONG).show();
    }

    @覆盖
    保护无效doInBackground(虚空...... PARAMS){
        //创建一个数组
        ArrayList的=新的ArrayList< ListModel的>();
        //从给定的URL地址JSON对象
        // Log.i(123,值1);
        的JSONObject = JSONfunctions.getJSONfromURL("http://firstchoicefood.in/fcfapiphpexpert/phpexpert_restaurantMenuItem.php?r=" + URLEn coder.en code(VALUENAME)+&放大器;渣油=+ URLEn coder.en code(valueid1)+&放大器; RestaurantCategoryID =+ URLEn codeR .EN code(valueid)+);



        尝试 {

            //找到在JSON数组名
            jsonarray = jsonobject.getJSONArray(RestaurantMenItems);
            Log.i(1234,+ JSONArray),其中;

            的for(int i = 0; I< jsonarray.length();我++){

                的JSONObject = jsonarray.getJSONObject(ⅰ);


                 ListModel的sched的=新的ListModel();
                sched.setProductName(jsonobject.getString(RestaurantPizzaItemName));

                sched.setPrice(jsonobject.getString(RestaurantPizzaItemPrice));

                arraylist.add(附表);

            }
        }赶上(JSONException E){
            Log.e(错误,e.getMessage());
            e.printStackTrace();
        }
        返回null;
    }

    @覆盖
    保护无效onPostExecute(无效参数){
        //定位在listview_main.xml列表视图
 最终的ListView列表视图=(ListView控件)findViewById(R.id.listViewdetails);

        适配器=新ListAdapterAddItems();

        listview.setAdapter(适配器);
        //关闭progressdialog
        mProgressDialog.dismiss();

        listview.setOnItemClickListener(新AdapterView.OnItemClickListener()
        {
            @覆盖
            公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT位置,长ARG3)
            {

                //获取人背后的点击项目
                ListModel的P =(ListModel的)listview.getItemAtPosition(位置);
                //登录等领域,以检查是否我们得到了我们想要的信息
                Log.i(SomeTag,人:+ p.getCount());
                Log.i(SomeTag,人的名字:+ p.getProductName());
                Log.i(SomeTag,Ruppees:+ p.getPrice());

                算上++;
                字符串countString =将String.valueOf(计数);
                吐司面包= Toast.makeText(getApplicationContext()
                        项目+(位置+ 1),
                        Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,0,0);
                toast.show();

               Toast.makeText(getBaseContext(),
                        countString,Toast.LENGTH_LONG).show();

            }
        });



    }
}

// ==========================
类ListAdapterAddItems扩展ArrayAdapter< ListModel的>
{
    ListAdapterAddItems(){
        超(DetaisRESTActivity.this,android.R.layout.simple_list_item_1,ArrayList的);
        // ImageLoader的=新ImageLoader的(MainActivity.this);
    }
    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        最后ViewHolder持有人;
        如果(convertView == NULL){
            LayoutInflater充气= getLayoutInflater();
            convertView = inflater.inflate(R.layout.cartlistitem,NULL);
            持有人=新ViewHolder(convertView);
            convertView.setTag(保持器);
        }其他{
            支架=(ViewHolder)convertView.getTag();
        }
        holder.populateFrom(arraylist.get(位置));
        返回convertView;
    }
}

类ViewHolder {
    公众的TextView restaurantname = NULL;
    公众的TextView ruppees = NULL;


    ViewHolder(查看行){
        restaurantname =(TextView中)row.findViewById(R.id.rastaurantnamedetailsrestaurant);
        ruppees =(TextView中)row.findViewById(R.id.rastaurantcuisinedetalsrestaurant);

    }

    //注意我们必须改变我们的populateFrom()采取的类型人的说法
    无效populateFrom(ListModel的R){
        restaurantname.setText(r.getProductName());
        ruppees.setText(r.getPrice());

    }


}














    // ================================================ =============






@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
    。getMenuInflater()膨胀(R.menu.menu_detais_rest,菜单);
    返回true;
}
 
360浏览器怎么把网站添加到允许弹出窗口的列表中

解决方案

添加一个全球性的 INT 变量类,并在你的的ListView onItemClickListener 递增这个变量 + 1 并获得引用您的 TextVie W和使用 TextView.setText(将String.valueOf(that_int_variable));

修改

看你的code一切首先让 mTitleTextView 是全球性的的TextView 对刚刚在OnCreate中,所以你可以参考它的对象。你看你在方法声明任何变量的生活,同时该方法被调用时。或者你可以让你的动作条,并使用 findViewById()让你的code应该是这样的。

 字符串valueid,valueid1,VALUENAME;
公众诠释计数= 0;
的JSONObject的JSONObject;
JSONArray jsonarray;
公众诠释listViewClickCounter = 0;
TextView的mTitleTextView; //这是编辑。您添加的TextView
ListAdapterAddItems适配器;
字符串restaurantmenuname,rastaurantname;
//那么其余部分将继续,我跳过所有
 

和,而不是设置文本马上在的TextView 设置时,项目已被点击

  listview.setOnItemClickListener(新AdapterView.OnItemClickListener()
    {
        @覆盖
        公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT位置,长ARG3)
        {

            //获取人背后的点击项目
            ListModel的P =(ListModel的)listview.getItemAtPosition(位置);
            //登录等领域,以检查是否我们得到了我们想要的信息
            Log.i(SomeTag,人:+ p.getCount());
            Log.i(SomeTag,人的名字:+ p.getProductName());
            Log.i(SomeTag,Ruppees:+ p.getPrice());

            算上++;
            字符串countString =将String.valueOf(计数);
            吐司面包= Toast.makeText(getApplicationContext()
                    项目+(位置+ 1),
                    Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,0,0);
            toast.show();

           Toast.makeText(getBaseContext(),
                    countString,Toast.LENGTH_LONG).show();
           mTitleTextView.setText(countString);

        }
    });
 

这就是你需要同时做你不使用这种 INT 变量 listViewClickCounter

Please help me how to add custom List view's number of clicks to custom action bar (textview) in android?

Here is my Activity Class

public class DetaisRESTActivity extends Activity {

String valueid,valueid1,valuename;
public int count=0;
JSONObject jsonobject;
JSONArray jsonarray;
public int listViewClickCounter=0;

ListAdapterAddItems adapter;

String restaurantmenuname,rastaurantname;

ProgressDialog mProgressDialog;
ArrayList<ListModel> arraylist;
public static String RASTAURANTNAMEDETAILS = "RestaurantPizzaItemName";
public static String RASTAURANTRUPPEES = "RestaurantPizzaItemPrice";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   // requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.activity_detais_rest);
 //   getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar);
    ActionBar actionBar = getActionBar();

    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ff0000")));

    LayoutInflater mInflater = LayoutInflater.from(this);

    View mCustomView = mInflater.inflate(R.layout.titlebar, null);
   TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.textView123456789);
    Toast.makeText(DetaisRESTActivity.this,listViewClickCounter, Toast.LENGTH_LONG).show();
    mTitleTextView.setText(listViewClickCounter);

    ImageButton imageButton = (ImageButton) mCustomView
            .findViewById(R.id.imageButton2);
    imageButton.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "Refresh Clicked!",
                    Toast.LENGTH_LONG).show();
            Intent i=new Intent(DetaisRESTActivity.this,QuentityActivity.class);
            startActivity(i);
        }
    });

    actionBar.setCustomView(mCustomView);
    actionBar.setDisplayShowCustomEnabled(true);
    Intent intent = getIntent();
    // get the extra value
    valuename = intent.getStringExtra("restaurantmenuname");
    valueid = intent.getStringExtra("restaurantmenunameid");
    valueid1 = intent.getStringExtra("idsrestaurantMenuId5");

    Log.i("valueid",valueid);
    Log.i("valuename",valuename);
    Log.i("valueid1",valueid1);

    new DownloadJSON().execute();
}


// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void,Void,Void> {

    @Override

    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(DetaisRESTActivity.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Android JSON Parse Tutorial");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
        Toast.makeText(DetaisRESTActivity.this, "Successs", Toast.LENGTH_LONG).show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Create an array
        arraylist = new ArrayList<ListModel>();
        // Retrieve JSON Objects from the given URL address
        //  Log.i("123",value1);
        jsonobject = JSONfunctions.getJSONfromURL("http://firstchoicefood.in/fcfapiphpexpert/phpexpert_restaurantMenuItem.php?r=" + URLEncoder.encode(valuename) + "&resid=" + URLEncoder.encode(valueid1) + "&RestaurantCategoryID=" + URLEncoder.encode(valueid) + "");



        try {

            // Locate the array name in JSON
            jsonarray = jsonobject.getJSONArray("RestaurantMenItems");
            Log.i("1234",""+jsonarray);

            for (int i = 0; i < jsonarray.length(); i++) {

                jsonobject = jsonarray.getJSONObject(i);


                 ListModel sched = new ListModel();
                sched.setProductName(jsonobject.getString("RestaurantPizzaItemName"));

                sched.setPrice(jsonobject.getString("RestaurantPizzaItemPrice"));

                arraylist.add(sched);

            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void args) {
        // Locate the listview in listview_main.xml
 final ListView   listview = (ListView) findViewById(R.id.listViewdetails);

        adapter = new ListAdapterAddItems();

        listview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
            {

                // Get Person "behind" the clicked item
                ListModel p =(ListModel)listview.getItemAtPosition(position);
                // Log the fields to check if we got the info we want
                Log.i("SomeTag", "Persons: " + p.getCount());
                Log.i("SomeTag", "Persons name: " + p.getProductName());
                Log.i("SomeTag", "Ruppees: " + p.getPrice());

                count++;
                String countString=String.valueOf(count);
                Toast toast = Toast.makeText(getApplicationContext(),
                        "Item " + (position + 1),
                        Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
                toast.show();

               Toast.makeText(getBaseContext(),
                        countString, Toast.LENGTH_LONG).show();

            }
        });



    }
}

//==========================
class ListAdapterAddItems extends ArrayAdapter<ListModel>
{
    ListAdapterAddItems(){
        super(DetaisRESTActivity.this,android.R.layout.simple_list_item_1,arraylist);
        //imageLoader = new ImageLoader(MainActivity.this);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(convertView == null){
            LayoutInflater inflater = getLayoutInflater();
            convertView = inflater.inflate(R.layout.cartlistitem, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        holder.populateFrom(arraylist.get(position));
        return convertView;
    }
}

class ViewHolder {
    public TextView restaurantname = null;
    public TextView ruppees = null;


    ViewHolder(View row) {
        restaurantname = (TextView) row.findViewById(R.id.rastaurantnamedetailsrestaurant);
        ruppees = (TextView) row.findViewById(R.id.rastaurantcuisinedetalsrestaurant);

    }

    // Notice we have to change our populateFrom() to take an argument of type "Person"
    void populateFrom(ListModel r) {
        restaurantname.setText(r.getProductName());
        ruppees.setText(r.getPrice());

    }


}














    //=============================================================






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

解决方案

Add a global int variable to your class, and in your ListView onItemClickListener increment this variable ++1 and get a reference to your TextView and use TextView.setText(String.ValueOf(that_int_variable));

EDIT

Looking at your code everything looks great, just some arrangements first of all let mTitleTextView be global TextView object on just in oncreate so you can reference it. You see any variable you declare in a method lives while the method is being called upon. or you can get your actionbar and use the findViewById() so your code should look like this

String valueid,valueid1,valuename;
public int count=0;
JSONObject jsonobject;
JSONArray jsonarray;
public int listViewClickCounter=0;
TextView mTitleTextView; // this is the edit. you add the textview
ListAdapterAddItems adapter;
String restaurantmenuname,rastaurantname;
//then the rest will continue, i skipped all that

and instead of setting the text right away in the TextView set it when an item has been clicked

listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
        {

            // Get Person "behind" the clicked item
            ListModel p =(ListModel)listview.getItemAtPosition(position);
            // Log the fields to check if we got the info we want
            Log.i("SomeTag", "Persons: " + p.getCount());
            Log.i("SomeTag", "Persons name: " + p.getProductName());
            Log.i("SomeTag", "Ruppees: " + p.getPrice());

            count++;
            String countString=String.valueOf(count);
            Toast toast = Toast.makeText(getApplicationContext(),
                    "Item " + (position + 1),
                    Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
            toast.show();

           Toast.makeText(getBaseContext(),
                    countString, Toast.LENGTH_LONG).show();
           mTitleTextView.setText(countString);

        }
    });

That is all you need to do also you do not use this int variable listViewClickCounter