我将计数定义为全局变量,我想在每次单击按钮时减少计数。这是我的代码:
这是主要的适配器代码调用adkhar_adapter.java
package com.nextar.al_nas.hussin_almo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.github.javiersantos.bottomdialogs.BottomDialog;
import java.util.ArrayList;
public class adkhar_adapter extends ArrayAdapter<adkarVar> {
private Context context;
int coun;
private static final String LOG_TAG = com.nextar.al_nas.hussin_almo.adkhar_adapter.class.getSimpleName();
/**
* This is our own custom constructor (it doesn't mirror a superclass constructor).
* The context is used to inflate the layout file, and the list is the data we want
* to populate into the lists.
*
* @param context The current context. Used to inflate the layout file.
* @param androidFlavors A List of AndroidFlavor objects to display in a list
*/
public adkhar_adapter(Activity context, ArrayList<adkarVar> androidFlavors) {
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
super(context, 0, androidFlavors);
this.context = context;
}
/**
* Provides a view for an AdapterView (ListView, GridView, etc.)
*
* @param position The position in the list of data that should be displayed in the
* list item view.
* @param convertView The recycled view to populate.
* @param parent The parent ViewGroup that is used for inflation.
* @return The View for the position in the AdapterView.
*/
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_adkhar, parent, false);
}
// Get the {@link AndroidFlavor} object located at this position in the list
final adkarVar currentAndroidFlavor = getItem(position);
Button share_btn = (Button) listItemView.findViewById(R.id.share);
final TextView adkhar = (TextView) listItemView.findViewById(R.id.adkhar);
coun= Integer.parseInt(currentAndroidFlavor.getVersionNumber());
share_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
coun--;
}
});
adkhar.setText(String.valueOf(currentAndroidFlavor.getVersionName()));
//لاضافة خط جديد
Typeface newfont = Typeface.createFromAsset(context.getAssets(), "fonts/FrutigerLTArabi.ttf");
adkhar.setTypeface(newfont);
//لاضافة خط جديد
Typeface newfont_button = Typeface.createFromAsset(context.getAssets(), "fonts/FrutigerLTArabi.ttf");
share_btn.setTypeface(newfont_button);
final TextView noadkhar = (TextView) listItemView.findViewById(R.id.no_adkhar);
noadkhar.setText(String.valueOf(coun));
// noadkhar.notifyDataSetChanged();
// Find the ImageView in the list_item.xml layout with the ID list_item_icon
final Button sheet = (Button) listItemView.findViewById(R.id.pop);
final Button open = (Button) listItemView.findViewById(R.id.share);
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Animation hyperspaceJump = AnimationUtils.loadAnimation(context, R.anim.hyperspace_jump);
open.startAnimation(hyperspaceJump);
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_TEXT, adkhar.getText().toString());
share.setType("text/plain");
context.startActivity(share);
}
});
sheet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Animation hyperspaceJump = AnimationUtils.loadAnimation(context, R.anim.hyperspace_jump);
sheet.startAnimation(hyperspaceJump);
new BottomDialog.Builder(context)
.setTitle(currentAndroidFlavor.getVersionNumber())
.setContent(currentAndroidFlavor.getVersionName())
.show();
}
});
sheet.setTypeface(newfont_button);
// int o=Integer.parseInt(adkhar.getText().toString().concat(open));
// j.setText();
// for (int i = 0; i < open.getChildCount(); i++) {
// final View view = open.getChildAt(i);
// view.setTag(i);
// view.setOnClickListener(new View.OnClickListener() {
//
// @Override
// public void onClick(View v) {
// }
// });
// }
// حركة جديد لللست فيو
Animation animation = AnimationUtils.loadAnimation(context, R.anim.fade_in);
listItemView.startAnimation(animation);
// Return the whole list item layout (containing 2 TextViews and an ImageView)
// so that it can be shown in the ListView
return listItemView;
}
}
这是适配器变量调用adkarVar.java
package com.nextar.al_nas.hussin_almo;
public class adkarVar {
// Name of the Android version (e.g. Gingerbread, Honeycomb, Ice Cream Sandwich)
private String mVersionName;
// Android version number (e.g. 2.3-2.7, 3.0-3.2.6, 4.0-4.0.4)
private String mVersionNumber;
// Drawable resource ID
private int mImageResourceId;
/*
* Create a new AndroidFlavor object.
*
* @param vName is the name of the Android version (e.g. Gingerbread)
* @param vNumber is the corresponding Android version number (e.g. 2.3-2.7)
* @param image is drawable reference ID that corresponds to the Android version
* */
public adkarVar(String vName, String vNumber, int imageResourceId)
{
mVersionName = vName;
mVersionNumber = vNumber;
mImageResourceId = imageResourceId;
}
/**
* Get the name of the Android version
*/
public String getVersionName() {
return mVersionName;
}
/**
* Get the Android version number
*/
public String getVersionNumber() {
return mVersionNumber;
}
/**
* Get the image resource ID
*/
public int getImageResourceId() {
return mImageResourceId;
}
}
这是主要活动调用adkaralsbah.java
package com.nextar.al_nas.hussin_almo;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.varunest.sparkbutton.SparkButton;
import com.varunest.sparkbutton.SparkEventListener;
import java.util.ArrayList;
public class adkaralsbah extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adkaralsbah);
ArrayList<adkarVar> androidFlavors = new ArrayList<adkarVar>();
androidFlavors.add(new adkarVar("الحَمْدُ لله الذِي أحْيَانا بَعْدَمَا أمَاتَنَا* وإلَيْهِ النَشُور", "1", R.drawable.ic_launcher_background));
androidFlavors.add(new adkarVar("الحَمْدُ لله الذِي عَافَانِي في جَسَدِي ورَدَّ عَلَيَّ رُوحِي، وأَذِنَ لي بِذِكْره", "1", R.drawable.ic_launcher_background));
androidFlavors.add(new adkarVar("مَنْ تَعَارَ مِنَ اللَّيْل* فقال: لا إلَهَ إلاَّ الله وحْدَهُ لا شَرِيكَ لَهُ، لَهُ المُلْكُ ولَهُ الحَمْدُ وهُوَ على كلِّ شيءٍ قَدير، الحَمْدُ لله وسُبْحانَ الله ولا إله إلا الله والله أكبر ولا حَولَ ولا قُوةَ إلا بالله ثم قال: اللَّهُمَّ اغْفِرْ لي ، أو دعى استُجيبَ لهُ، فإن توَضأَ وصَلّى قُبِلَتْ صَلاتُهُ.", "1", R.drawable.ic_launcher_background));
androidFlavors.add(new adkarVar("{إِنَّ فِي خَلْقِ السَّمَاوَاتِ وَالأَرْضِ وَاخْتِلاَفِ اللَّيْلِ وَالنَّهَارِ لآيَاتٍ لِّأُوْلِي الألْبَابِ *الَّذِينَ يَذْكُرُونَ اللّهَ قِيَاماً وَقُعُوداً وَعَلَىَ جُنُوبِهِمْ وَيَتَفَكَّرُونَ فِي خَلْقِ السَّمَاوَاتِ وَالأَرْضِ رَبَّنَا مَا خَلَقْتَ هَذا بَاطِلاً سُبْحَانَكَ فَقِنَا عَذَابَ النَّارِ*رَبَّنَا إِنَّكَ مَن تُدْخِلِ النَّارَ فَقَدْ أَخْزَيْتَهُ وَمَا لِلظَّالِمِينَ مِنْ أَنصَارٍ *رَّبَّنَا إِنَّنَا سَمِعْنَا مُنَادِياً يُنَادِي لِلإِيمَانِ أَنْ آمِنُواْ بِرَبِّكُمْ فَآمَنَّا رَبَّنَا فَاغْفِرْ لَنَا ذُنُوبَنَا وَكَفِّرْ عَنَّا سَيِّئَاتِنَا وَتَوَفَّنَا مَعَ الأبْرَارِ * رَبَّنَا وَآتِنَا مَا وَعَدتَّنَا عَلَى رُسُلِكَ وَلاَ تُخْزِنَا يَوْمَ الْقِيَامَةِ إِنَّكَ لاَ تُخْلِفُ الْمِيعَادَ *فَاسْتَجَابَ لَهُمْ رَبُّهُمْ أَنِّي لاَ أُضِيعُ عَمَلَ عَامِلٍ مِّنكُم مِّن ذَكَرٍ أَوْ أُنثَى بَعْضُكُم مِّن بَعْضٍ فَالَّذِينَ هَاجَرُواْ وَأُخْرِجُواْ مِن دِيَارِهِمْ وَأُوذُواْ فِي سَبِيلِي وَقَاتَلُواْ وَقُتِلُواْ لأُكَفِّرَنَّ عَنْهُمْ سَيِّئَاتِهِمْ وَلأُدْخِلَنَّهُمْ جَنَّاتٍ تَجْرِي مِن تَحْتِهَا الأَنْهَارُ ثَوَاباً مِّن عِندِ اللّهِ وَاللّهُ عِندَهُ حُسْنُ الثَّوَابِ *لاَ يَغُرَّنَّكَ تَقَلُّبُ الَّذِينَ كَفَرُواْ فِي الْبِلاَدِ *مَتَاعٌ قَلِيلٌ ثُمَّ مَأْوَاهُمْ جَهَنَّمُ وَبِئْسَ الْمِهَادُ *لَكِنِ الَّذِينَ اتَّقَوْاْ رَبَّهُمْ لَهُمْ جَنَّاتٌ تَجْرِي مِن تَحْتِهَا الأَنْهَارُ خَالِدِينَ فِيهَا نُزُلاً مِّنْ عِندِ اللّهِ وَمَا عِندَ اللّهِ خَيْرٌ لِّلأَبْرَارِ * وَإِنَّ مِنْ أَهْلِ الْكِتَابِ لَمَن يُؤْمِنُ بِاللّهِ وَمَا أُنزِلَ إِلَيْكُمْ وَمَا أُنزِلَ إِلَيْهِمْ خَاشِعِينَ لِلّهِ لاَ يَشْتَرُونَ بِآيَاتِ اللّهِ ثَمَناً قَلِيلاً أُوْلَـئِكَ لَهُمْ أَجْرُهُمْ عِندَ رَبِّهِمْ إِنَّ اللّهَ سَرِيعُ الْحِسَابِ *يَا أَيُّهَا الَّذِينَ آمَنُواْ اصْبِرُواْ وَصَابِرُواْ وَرَابِطُواْ وَاتَّقُواْ اللّهَ لَعَلَّكُمْ تُفْلِحُونَ }", "1", R.drawable.ic_launcher_background));
// Create an {@link AndroidFlavorAdapter}, whose data source is a list of
// {@link AndroidFlavor}s. The adapter knows how to create list item views for each item
// in the list.
adkhar_adapter flavorAdapter = new adkhar_adapter(this, androidFlavors);
//لاضافة خط جديد
TextView name=(TextView)findViewById(R.id.name);
Typeface newfont_button = Typeface.createFromAsset(this.getAssets(), "fonts/FrutigerLTArabi.ttf");
name.setTypeface(newfont_button);
// Get a reference to the ListView, and attach the adapter to the listView.
ListView listView = (ListView) findViewById(R.id.hussn);
listView.setAdapter(flavorAdapter);
SparkButton spark=(SparkButton) findViewById(R.id.spark_button);
spark.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Intent i=new Intent(adkaralsbah.this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i); }
});
}
}
这是我的代码的一部分,请帮助我
当我单击share_btn按钮时,它应将计数减少1,但没有任何变化。我怎么了请帮助我
答案 0 :(得分:0)
更改数据后,您必须通知适配器。使用:-
noadkhar.setText(String.valueOf(coun));
adkhar_adapter.notifyDataSetChanged();