这是我的MainActivity.java:
public class MainActivity extends AppCompatActivity {
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance ();
DatabaseReference databaseReference = firebaseDatabase.getReference ();
private JazzyListView listView;
int check=0;
MyAdapter adapter;
ProgressDialog progressDialog;
List<String> ListString = new ArrayList<String> ();
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main22);
final PullToRefreshView pullToRefreshView=(PullToRefreshView) findViewById (R.id.pull);
pullToRefreshView.setOnRefreshListener (new PullToRefreshView.OnRefreshListener () {
@Override
public void onRefresh() {
ListString.clear ();
databaseReference.addValueEventListener (new ValueEventListener () {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
DataSnapshot snapshot = dataSnapshot.child ("User");
for(DataSnapshot s: snapshot.getChildren ())
{
if(s.getValue ().toString ().isEmpty ())
Toast.makeText (MainActivity.this, "Empty", Toast.LENGTH_SHORT).show ();
else
ListString.add(s.getKey ().toString ());
}
adapter.notifyDataSetChanged ();
pullToRefreshView.setRefreshing (false);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText (MainActivity.this, databaseError.getMessage (), Toast.LENGTH_SHORT).show ();
}
});
}
});
Connectivity connectivity = new Connectivity (MainActivity.this);
if(connectivity.isConnected ()){
progressDialog = new ProgressDialog (MainActivity.this);
progressDialog.setCancelable (false);
progressDialog.setMessage ("Loading");
progressDialog.setTitle ("Retrieving Data");
progressDialog.show ();
ListString.add ("Adding");
adapter = new MyAdapter (MainActivity.this, ListString);
databaseReference.addValueEventListener (new ValueEventListener () {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
check++;
if(check<=1) {
ListString.remove ("Adding");
DataSnapshot snapshot = dataSnapshot.child ("User");
for(DataSnapshot s: snapshot.getChildren ())
{
if(s.getValue ().toString ().isEmpty ())
Toast.makeText (MainActivity.this, "Empty", Toast.LENGTH_SHORT).show ();
else
ListString.add(s.getKey ().toString ());
}
progressDialog.dismiss ();
adapter.notifyDataSetChanged ();
}else{
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText (MainActivity.this, databaseError.getMessage (), Toast.LENGTH_SHORT).show ();
}
});
}else{
AlertDialog.Builder builder = new AlertDialog.Builder (MainActivity.this);
builder.setTitle("No Network")
.setMessage ("No Network Available").setPositiveButton ("Ok", new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss ();
}
}) ;
AlertDialog alertDialog = builder.create ();
alertDialog.show ();
}
listView = (JazzyListView) findViewById (R.id.listview);
listView.setTransitionEffect (new FadeEffect ());
listView.setAdapter (adapter);
//adapter.notifyDataSetChanged ();
listView.setOnItemClickListener (new AdapterView.OnItemClickListener () {
@Override
public void onItemClick(final AdapterView<?> adapterView, final View view, int i, long l) {
final String itemname = adapterView.getItemAtPosition (i).toString ();
try{
AlertDialog.Builder builder = new AlertDialog.Builder (MainActivity.this);
builder.setTitle("Present?")
.setMessage ("Present or Absent:").setPositiveButton ("Present", new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
try{
//view.setBackgroundColor (getColor (android.R.color.holo_green_light));
Calendar calendar = Calendar.getInstance ();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("dd MMM yyyy");
DatabaseReference user = databaseReference.child ("Attendence");
DatabaseReference date = user.child (simpleDateFormat.format (calendar.getTime ()));
DatabaseReference name = date.child (itemname);
name.setValue ("Present");
}catch (Exception e){
Toast.makeText (MainActivity.this, e.getMessage (), Toast.LENGTH_SHORT).show ();
}
}
}).setNegativeButton ("Absent", new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Calendar calendar = Calendar.getInstance ();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("dd MMM yyyy");
DatabaseReference user = databaseReference.child ("Attendence");
DatabaseReference date = user.child (simpleDateFormat.format (calendar.getTime ()));
DatabaseReference name = date.child (itemname);
name.setValue ("Absent");
}
});
AlertDialog alertDialog = builder.create ();
alertDialog.show ();
}catch (Exception e){
Toast.makeText (MainActivity.this, e.getMessage (), Toast.LENGTH_SHORT).show ();
}
}
});
}
}
这是我的MyAdapter.java文件:
public class MyAdapter extends BaseAdapter{
List thisList;
Context thiscontext;
LayoutInflater inflater;
public MyAdapter(Context context,List list) {
thiscontext = context;
thisList = list;
inflater= (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return thisList.size ();
}
@Override
public Object getItem(int i) {
return thisList.get (i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflater.inflate (R.layout.list_main, null);
TextView textView = (TextView) view.findViewById (R.id.textView);
String upper = thisList.get (i).toString ().substring (0,1).toUpperCase () + thisList.get (i).toString ().substring (1);
TextView boxText = (TextView) view.findViewById (R.id.boxText);
TextView boxView = (TextView) view.findViewById (R.id.boxView);
String first = thisList.get (i).toString ().toUpperCase ().substring (0,1);
textView.setText (upper);
boxText.setText (first);
return view;
}
}
我想这样做,当用户点击列表项时,列表应该通过MainActivity.java将礼物发送到MyAdapter.java类中的boxView TextView,然后boxView应该在每个项目中设置当前或不存在的ListView。
答案 0 :(得分:0)
首先,您首先在MyAdapter类中创建一个新的布尔变量。然后,您创建新方法setPresent(),并使用它来设置所需的布尔值。
您的代码已更改:
在活动中
builder.setTitle("Present?")
.setMessage ("Present or Absent:").setPositiveButton ("Present", new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
try{
//view.setBackgroundColor (getColor (android.R.color.holo_green_light));
Calendar calendar = Calendar.getInstance ();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("dd MMM yyyy");
DatabaseReference user = databaseReference.child ("Attendence");
DatabaseReference date = user.child (simpleDateFormat.format (calendar.getTime ()));
DatabaseReference name = date.child (itemname);
name.setValue ("Present");
adapterView.updateView(true, i); // to get the present value form your alert;
}catch (Exception e){
Toast.makeText (MainActivity.this, e.getMessage (), Toast.LENGTH_SHORT).show ();
}
}
}).setNegativeButton ("Absent", new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Calendar calendar = Calendar.getInstance ();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("dd MMM yyyy");
DatabaseReference user = databaseReference.child ("Attendence");
DatabaseReference date = user.child (simpleDateFormat.format (calendar.getTime ()));
DatabaseReference name = date.child (itemname);
name.setValue ("Absent");
adapterView.updateView(false, i); // to get the absent value form your alert;
}
});
在适配器中;
public class MyAdapter extends BaseAdapter{
List thisList;
Context thiscontext;
LayoutInflater inflater;
public MyAdapter(Context context,List list) {
thiscontext = context;
thisList = list;
inflater= (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return thisList.size ();
}
@Override
public Object getItem(int i) {
return thisList.get (i);
}
@Override
public long getItemId(int i) {
return i;
}
// new method
private void updateView(Boolean value, int index){
View v = thisList.getChildAt(index -
thisList.getFirstVisiblePosition());
if(v == null)
return;
TextView boxView = (TextView) v.findViewById (R.id.boxView);
if (value && value != null) {
boxView.setText("Present");
} else if (!value && value !=null) {
boxView.setText("Absent");
}
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflater.inflate (R.layout.list_main, null);
TextView textView = (TextView) view.findViewById (R.id.textView);
String upper = thisList.get (i).toString ().substring (0,1).toUpperCase () + thisList.get (i).toString ().substring (1);
TextView boxText = (TextView) view.findViewById (R.id.boxText);
TextView boxView = (TextView) view.findViewById (R.id.boxView);
String first = thisList.get (i).toString ().toUpperCase ().substring (0,1);
textView.setText (upper);
boxText.setText (first);
return view;
}
}
设置值后,您应通知适配器,行中有更改。那就是它!你传递了布尔值。
答案 1 :(得分:0)
这是我通过思考得到的答案,实际上对我有用:
而不是使用布尔我使用HashMap函数并为不同的项目位置存储不同的值:
map.put (String.valueOf (postion), "1");
其中postion是整数中项目的位置。
map.put (String.valueOf (postion), "0");
然后我在MyAdapter.java中创建了函数setMap,它设置了MyAdapter.java的hashMap值
public void setPresent(Map map1){
map = map1;
Log.e ("Inner Length", String.valueOf (map.toString ()));
}
并访问不同位置的值让我做我想做的事。