我正在创建一个应用程序,使用下一个按钮显示字符串到下一个。但我的方法根本不显示字符串。我需要使用 <TextView
android:id="@+id/fact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="246dp"
android:text=" "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/header" />
方法吗?
public class FunFactsActivity extends AppCompatActivity {
Button back;
Button next;
TextView fact;
private List<String> listOfFacts;
@Override
protected void onCreate( Bundle savedInstanceState) {
setContentView(R.layout.activity_fun_facts);
super.onCreate(savedInstanceState);
fact = findViewById(R.id.fact);
back = findViewById(R.id.back);
next = findViewById(R.id.next);
listOfFacts = new ArrayList<>();
//adds facts to the listOfFacts.
for (int i = 0; i < new Database().facts.length; i++) {
listOfFacts.add(new Database().facts[i]);
}
final String firstFact = listOfFacts.get(0);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String nextFact;
nextFact = firstFact+1;
displayFact(nextFact);
}
});
}
public String displayFact(String fact){
return fact;
}// display all of the facts one at a time, in order, go to the next one using the "next" button.
}
活动代码
import asyncio
import asyncpg
import requests
class node_http_mtr():
def __init__(self, ip, nsrc, ndst):
self.ip = ip
self.nsrc = nsrc
self.ndst = ndst
try:
self.data = requests.get('http://' + self.ip + '/nph-cgi_mtr?duration=-1&interval=0', stream=True, timeout=10)
except:
return
def __iter__(self):
return self
def __next__(self):
mtr = list()
try:
for chunk in self.data.iter_content(32 * (self.nsrc + self.ndst), '\n'):
# DEBUG log chunk
for line in chunk.split('\n'):
# DEBUG log line
if line.startswith('MTR'):
try:
_, io, num, val = line.split(' ')
l, r = val.split(':')[1], val.split(':')[2]
mtr.append((self.ip, io+num, l, r))
except:
# ERROR log line
pass
if len(mtr) == self.nsrc + self.ndst:
break
if len(mtr) == self.nsrc + self.ndst:
yield mtr
else:
continue
except:
# ERROR connection lost
return
async def save_to_db(data_to_save):
global pool
try:
async with pool.acquire() as conn:
await conn.execute('''INSERT INTO mtr (ip, io, l, r) VALUES %s''' % ','.join(str(row) for row in data_to_save))
finally:
await pool.release(conn)
async def remove_from_db(ip):
global pool
try:
async with pool.acquire() as conn:
await conn.execute('''DELETE FROM httpmtr WHERE ip = $1''', ip)
finally:
await pool.release(conn)
async def http_mtr_worker():
global workers_list
global loop
while True:
await asyncio.sleep(0)
for ip in list(workers_list):
data_to_save = next(workers_list[ip])
if data_to_save:
asyncio.ensure_future(save_to_db(next(data_to_save)))
await asyncio.sleep(0)
async def check_for_workers():
global workers_list
global pool
while True:
await asyncio.sleep(0)
try:
async with pool.acquire() as conn:
workers = await conn.fetch('''SELECT ip FROM httpmtr''')
finally:
await pool.release(conn)
for worker in workers:
if worker['ip'] not in list(workers_list):
workers_list[worker['ip']] = node_http_mtr(worker['ip'], 8, 8)
await asyncio.sleep(0)
print('Add worker', worker['ip'])
await asyncio.sleep(0)
ips_to_delete = set(workers_list.keys()) - set([i[0] for i in workers])
if len(ips_to_delete) != 0:
for ip in ips_to_delete:
print('Delete worker ', ip)
workers_list.pop(ip)
await asyncio.sleep(0)
async def make_db_connection():
pool = await asyncpg.create_pool(user='postgres', password='data', database='test', host='localhost', max_queries=50000, command_timeout=60)
return pool
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
pool = loop.run_until_complete(make_db_connection())
workers_list = {}
try:
asyncio.ensure_future(check_for_workers())
asyncio.ensure_future(http_mtr_worker())
loop.run_forever()
except Exception as e:
print(e)
pass
finally:
print("Closing Loop")
loop.close()
答案 0 :(得分:1)
这是你的问题:
public String displayFact(String fact){
return fact;
}
更改为:
public void displayFact(String fact){
this.fact.setText(fact);
}
另外,宣布一个计数器作为班级成员来跟踪你的事实:
private int counter;
@Override
protected void onCreate( Bundle savedInstanceState) {
...
counter = 0;
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
counter += 1
String nextFact = listOfFacts.get(counter);
displayFact(nextFact);
}
});