在一个活动中,我有一个ListView和一个GridView布局。 GridView布局在运行时使用按钮填充,具体取决于数据库内容,并且每个按钮都会创建onClick回调。 我想在每次点击按钮时用不同的项目更新ListView,但我仍然坚持如何从Button onClick更新ListView。 这是我的代码:
BlagajnaActivity.class
public class BlagajnaActivity extends AppCompatActivity {
GridView myBtngridView;
ListView racunListView;
ArrayList racunList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Blagajna");
setContentView(R.layout.activity_blagajna);
DatabaseHelper database = new DatabaseHelper(this);
SQLiteDatabase db = database.getReadableDatabase();
String[] projection = {
ArtiklData.ArtiklDescription._ID,
ArtiklData.ArtiklDescription.ARTIKL,
ArtiklData.ArtiklDescription.KOLICINA_NA_SKLADISTU,
ArtiklData.ArtiklDescription.CIJENA,
ArtiklData.ArtiklDescription.TIP
};
String sortOrder = ArtiklData.ArtiklDescription.ARTIKL + " ASC";
Cursor cursor = db.query(
ArtiklData.ArtiklDescription.TABLE_NAME,
projection,
null,
null,
null,
null,
sortOrder
);
RacunListViewAdapter racun_adapter = new RacunListViewAdapter(this, R.layout.racun_layout, racunList);
racunListView = (ListView) findViewById(R.id.LV_Racun);
racunListView.setAdapter(racun_adapter);
racun_adapter.notifyDataSetChanged();
ArtiklData newBtnArtikl;
ArrayList artiklNamesList = new ArrayList<>();
while (cursor.moveToNext()) {
String artikl = cursor.getString(cursor.getColumnIndexOrThrow(ArtiklData.ArtiklDescription.ARTIKL));
Integer kolicina = cursor.getInt(cursor.getColumnIndexOrThrow(ArtiklData.ArtiklDescription.KOLICINA_NA_SKLADISTU));
Integer cijena = cursor.getInt(cursor.getColumnIndexOrThrow(ArtiklData.ArtiklDescription.CIJENA));
Integer itip = cursor.getInt(cursor.getColumnIndexOrThrow(ArtiklData.ArtiklDescription.TIP));
Boolean tip = (itip == 1) ? true : false;
newBtnArtikl = new ArtiklData(artikl, kolicina, cijena, tip);
artiklNamesList.add(newBtnArtikl);
}
CustGridViewAdapter adapter_btn = new CustGridViewAdapter(this, artiklNamesList);
myBtngridView = (GridView) findViewById(R.id.GV_Artikli);
myBtngridView.setAdapter(adapter_btn);
cursor.close();
db.close();
}
public class CustGridViewAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<ArtiklData> artikli;
public CustGridViewAdapter(Context context, ArrayList<ArtiklData> list) {
this.mContext = context;
this.artikli = list;
}
@Override
public int getCount() {
return artikli.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Button btn;
ArtiklData artikl = artikli.get(position);
if (convertView == null) {
btn = new Button(mContext);
btn.setLayoutParams(new GridView.LayoutParams(85, 85));
ViewGroup.LayoutParams p = btn.getLayoutParams();
p.width = ViewGroup.LayoutParams.WRAP_CONTENT;
btn.setLayoutParams(p);
btn.setPadding(8, 8, 8, 8);
} else {
btn = (Button) convertView;
}
String mybuttontext = artikli.get(position).getArtikl().toUpperCase() + System.getProperty("line.separator") + System.getProperty("line.separator") + artikli.get(position).getCijena() + " Kn";
btn.setText(mybuttontext);
btn.setTransformationMethod(null);
btn.setTextColor(Color.WHITE);
btn.setId(position);
btn.setOnClickListener(new ArtiklBtnOnClickListener(position));
return btn;
}
}
}
ArtiklBtnOnClickListener.class
public class ArtiklBtnOnClickListener implements View.OnClickListener {
private final int position;
public ArtiklBtnOnClickListener(int position) {
this.position = position;
}
public void onClick(View view) {
Button BtnStisnut = (Button) view;
String ArtiklCijena = BtnStisnut.getText().toString();
String lines[] = ArtiklCijena.split("\\r?\\n");
String name = lines[0];
String linescijena[] = lines[2].split("\\s+");
Integer price = Integer.parseInt(linescijena[0]);
DatabaseHelper database = new DatabaseHelper(view.getContext());
SQLiteDatabase db = database.getReadableDatabase();
String[] return_columns = {
ArtiklData.ArtiklDescription._ID,
ArtiklData.ArtiklDescription.ARTIKL,
ArtiklData.ArtiklDescription.KOLICINA_NA_SKLADISTU,
ArtiklData.ArtiklDescription.CIJENA,
ArtiklData.ArtiklDescription.TIP
};
String whereClause = ArtiklData.ArtiklDescription.ARTIKL + "=?";
String[] whereArgs = {name.toUpperCase()};
Cursor cursor = db.query(
ArtiklData.ArtiklDescription.TABLE_NAME,
return_columns,
whereClause,
whereArgs,
null,
null,
null
);
String artikl;
Integer kolicina, cijena;
if (cursor.getCount() == 1) {
while (cursor.moveToNext()) {
artikl = cursor.getString(cursor.getColumnIndexOrThrow(ArtiklData.ArtiklDescription.ARTIKL));
kolicina = cursor.getInt(cursor.getColumnIndexOrThrow(ArtiklData.ArtiklDescription.KOLICINA_NA_SKLADISTU));
cijena = cursor.getInt(cursor.getColumnIndexOrThrow(ArtiklData.ArtiklDescription.CIJENA));
}
}
cursor.close();
db.close();
}
}
你能帮忙吗?
答案 0 :(得分:2)
在适配器中实现一个接口,并将其传递给ClickListener,并在onClick
中的接口中调用方法interface GridClickCallback{
void onGridItemClicked();
}
适配器类
public class CustGridViewAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<ArtiklData> artikli;
private GridClickCallback callback;
public CustGridViewAdapter(Context context, ArrayList<ArtiklData> list,GridClickCallback callback) {
this.mContext = context;
this.artikli = list;
this.callback = callback;
}
@Override
public int getCount() {
return artikli.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Button btn;
ArtiklData artikl = artikli.get(position);
if (convertView == null) {
btn = new Button(mContext);
btn.setLayoutParams(new GridView.LayoutParams(85, 85));
ViewGroup.LayoutParams p = btn.getLayoutParams();
p.width= ViewGroup.LayoutParams.WRAP_CONTENT;
btn.setLayoutParams(p);
btn.setPadding(8, 8, 8, 8);
}
else {
btn = (Button) convertView;
}
String mybuttontext=artikli.get(position).getArtikl().toUpperCase()+System.getProperty("line.separator")+System.getProperty("line.separator")+artikli.get(position).getCijena()+" Kn";
btn.setText(mybuttontext);
btn.setTransformationMethod(null);
btn.setTextColor(Color.WHITE);
btn.setId(position);
btn.setOnClickListener(new ArtiklBtnOnClickListener(position),callback); //<-- pass it to click listener
return btn;
}
ArtiklBtnOnClickListener
public class ArtiklBtnOnClickListener implements View.OnClickListener {
private final int position;
private GridClickCallback callback;
public ArtiklBtnOnClickListener(int position,GridClickCallback callback)
{
this.position = position;
this.callback = callback;
}
public void onClick(View view) {
Button BtnStisnut = (Button)view;
String ArtiklCijena = BtnStisnut.getText().toString();
String lines[] = ArtiklCijena.split("\\r?\\n");
String name=lines[0];
String linescijena[]=lines[2].split("\\s+");
Integer price=Integer.parseInt(linescijena[0]);
DatabaseHelper database = new DatabaseHelper(view.getContext());
SQLiteDatabase db = database.getReadableDatabase();
String[] return_columns = {
ArtiklData.ArtiklDescription._ID,
ArtiklData.ArtiklDescription.ARTIKL,
ArtiklData.ArtiklDescription.KOLICINA_NA_SKLADISTU,
ArtiklData.ArtiklDescription.CIJENA,
ArtiklData.ArtiklDescription.TIP
};
String whereClause = ArtiklData.ArtiklDescription.ARTIKL+"=?";
String [] whereArgs = {name.toUpperCase()};
Cursor cursor = db.query(
ArtiklData.ArtiklDescription.TABLE_NAME,
return_columns,
whereClause,
whereArgs,
null,
null,
null
);
String artikl;
Integer kolicina, cijena;
if (cursor.getCount() == 1){
while(cursor.moveToNext()) {
artikl = cursor.getString(cursor.getColumnIndexOrThrow(ArtiklData.ArtiklDescription.ARTIKL));
kolicina = cursor.getInt(cursor.getColumnIndexOrThrow(ArtiklData.ArtiklDescription.KOLICINA_NA_SKLADISTU));
cijena = cursor.getInt(cursor.getColumnIndexOrThrow(ArtiklData.ArtiklDescription.CIJENA));
}
}
cursor.close();
db.close();
callback.onGridItemClicked(); // <---- call it here
}
}
然后在活动
CustGridViewAdapter adapter_btn = new CustGridViewAdapter(this, artiklNamesList,new GridClickCallback(){
@Override
public void onGridItemClicked(){
// do modification to list
}
});
如果需要,您也可以在界面方法中传递数据
答案 1 :(得分:0)
在我的适配器
中 file = open('atf_log_28378.txt', "r")
for result in re.findall('^--- /b/act-builder/(.*?)Error code', file.read(), re.S):
print 'Failed test cases:' + result + '\n'
Expected Output will be the one or more combination of these:-
--- /b/act-builder/sandboxes/ACT-SB-RELEASE_174_THROTTLE-28378/src/junos/usr.sbin/rpd/lib/policy/atf-tests.host,linux ---
test1: [0.040247s] Failed: Test program received signal 6
test2: [0.039347s] Failed: Test program received signal 6
[0.079804s]
scu_dcu_test (12/12): 1 test cases
test1: [0.039158s] Failed: Test program received signal 6
[0.039262s]
Failed test cases:
aspath_test:test1, aspath_test:test2, aspath_test:test3, aspath_test:test4, cbf_test:test1, com_test:test1, com_test:test2, cmgr_test:test1, cmgr_test:test2, lsp_regex_test:test1, match_cache_test:test1, match_cache_test:test2, match_cache_test:test3, match_cache_test:test4, match_cache_test:test5, mt_test:test1, path_attr_err_test:test1, path_attr_err_test:test2, plist_test:test1, policy_namelist_test:test1, policy_te_test:test1, policy_te_test:test2, scu_dcu_test:test1
Summary for 12 test programs:
0 passed test cases.
23 failed test cases.
0 expected failed test cases.
0 skipped test cases.
*** [atf-run.log] Error code 1 (ignored)
然后在你的按钮的监听器中,你打电话
public class CustGridViewAdapter extends BaseAdapter {
private Context mContext;
private List<Elements> elements;
public CustGridViewAdapter(Context context, ArrayList<Elements> list) {
this.mContext = context;
this.elements = list;
}
//other methods here
//getView
//getItemId
//getItem
@Override
public int getCount() {
return elements.size();
}
public void updateElements(ArrayList<Elements> elements){
this.list.clear();
this.list.addAll(elements);
notifyDataSetChanged();
}